2016-07-28 144 views
0

我有以下代码在wordpress中用自定义分类过滤。但它不工作。wordpress -filter自定义分类不工作

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'); 

回答

0

根据WP_Query DOC,tax_query是数组的数组:

重要提示: tax_query需要税查询参数阵列 (它需要一个数组的数组)的数组.. ..

所以,请试试这个:

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'); 

我希望这会有所帮助。

+0

我的问题是存在只有主题切换使用multy主题插件https://wordpress.org/plugins/jonradio-multiple-themes/ – Shin