简体   繁体   中英

wordpress -filter with custom taxonomy not working

I have the bellow code for filter with a custom taxonomy in wordpress.but it is not working.

    function alter_query_so_15250127($query) {
            $tax_query = array(
            'taxonomy' => 'demographic',             
            'field' => 'id',                  
            'terms' => array( 522 ),    
            'operator' => 'IN'                    
       );
     $query->set( 'tax_query', $tax_query );
    }
    add_action('pre_get_posts','alter_query_so_15250127');

According to WP_Query doc, tax_query is an array of arrays:

Important Note: tax_query takes an array of tax query arguments arrays (it takes an array of arrays)....

So, please try this:

function alter_query_so_15250127($query) {
        //Add wrapper here
        $tax_query = array( array(
        'taxonomy' => 'demographic',             
        'field' => 'id',                  
        'terms' => array( 522 ),    
        'operator' => 'IN'                    
   ) );
 $query->set( 'tax_query', $tax_query );
}
add_action('pre_get_posts','alter_query_so_15250127');

I hope this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM