我认为最好的解决办法是pre_get_posts
行动挂钩。首先,检查我们是否在自定义分类法的存档中,然后设置include_children
到false
对于tax_query
argument 查询的。
add_action( \'pre_get_posts\', \'cyb_pre_get_posts\' );
function cyb_pre_get_posts( $query ) {
//Assuming the slug of the custom taxonomy is company-category
//change it with the correct value if needed
if( $query->is_tax( \'company-category\' ) && $query->is_main_query() && !is_admin() ) {
$tax_query = array(
array(
\'taxonomy\' => \'company-category\',
\'terms\' => $query->get( \'company-category\' ),
\'include_children\' => false
);
$query->set( \'tax_query\', $tax_query );
}
}
另一种似乎更好的方法
answer 并适用于自定义分类法,而不是核心类别分类法):
add_filter( \'parse_tax_query\', \'cyb_do_not_include_children_in_company_category_archive\' );
function cyb_do_not_include_children_in_company_category_archive( $query ) {
if (
! is_admin()
&& $query->is_main_query()
&& $query->is_tax( \'company-category\' )
) {
$query->tax_query->queries[0][\'include_children\'] = 0;
}
}
有关自定义查询和辅助循环(请参见
WP_Query):
$args = array(
//Rest of you args go here
\'tax_query\' => array(
array(
\'include_children\' => false
)
)
);
$query = new WP_Query( $args );