如何使用另一个自定义分类的术语过滤自定义分类页面上的帖子

时间:2019-10-23 05:40:14

标签: wordpress custom-taxonomy taxonomy-terms

我有一个自定义帖子类型-想法。它有2种自定义分类法-idea_cateducation_level

现在,我位于idea_cat的存档页面上,想使用education_levelpre_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' );

0 个答案:

没有答案