我有一个自定义帖子类型-想法。它有2种自定义分类法-idea_cat
和education_level
。
现在,我位于idea_cat
的存档页面上,想使用education_level
从pre_get_posts
的术语中过滤帖子。
我正在使用此代码-
function target_main_category_query_with_conditional_tags( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_tax( 'idea_cat' ) ) {
if ( isset( $_GET['term_education_level'] ) && $_GET['term_education_level'] != '' ) {
$tax_query = array(
array(
'taxonomy' => 'idea_cat',
'field' => 'slug',
'terms' => $query->query['idea_cat'],
'operator' => 'IN',
'include_children' => 1
),
array(
'taxonomy' => 'education_level',
'field' => 'slug',
'terms' => esc_html( $_GET['term_education_level'] )
)
);
$query->tax_query->queries = $tax_query;
}
}
}
}
add_action( 'pre_get_posts', 'target_main_category_query_with_conditional_tags' );