是否使用PRE_GET_POSTS排除发布类型?

时间:2013-07-07 作者:psot

我正在使用pre\\u get\\u posts进行自定义分类法归档显示。现在,我添加了一个帖子类型“photo”,它与默认的帖子类型共享相同的分类法。如何通过更新现有函数排除分类法归档循环获取的“照片”帖子?

function my_breakfast_query ( $query ) {
// not an admin page and is the main query
if (!is_admin() && $query->is_main_query()){
if (is_tax( \'food\', \'breakfast\' )){
  $tax_query = array(
    \'relation\' => \'OR\',
    array(
      \'taxonomy\' => \'category\',
         \'field\' => \'id\',
         \'terms\' => array( 366 )
    ),
    array(
        \'taxonomy\' => \'food\',
         \'field\' => \'id\',
         \'terms\' => array( 364 )
    )
  );
  $query->set(\'tax_query\', $tax_query);
 }
 }
}
add_action( \'pre_get_posts\', \'my_breakfast_query\' );

1 个回复
最合适的回答,由SO网友:psot 整理而成

我自己找到了解决办法。它返回默认的帖子类型,而不是排除自定义的帖子类型,这很适合我的需要。

function my_breakfast_query ( $query ) {
// not an admin page and is the main query
if (!is_admin() && $query->is_main_query()){
if (is_tax( \'food\', \'breakfast\' )){
  $tax_query = array(
    \'relation\' => \'OR\',
     array(
        \'taxonomy\' => \'category\',
        \'field\' => \'id\',
        \'terms\' => array( 366 )
     ),
     array(
        \'taxonomy\' => \'food\',
        \'field\' => \'id\',
        \'terms\' => array( 364 )
     )
  );
  $query->set(\'post_type\',\'post\'); // Added this line to get a working solution
  $query->set(\'tax_query\', $tax_query);
 }
 return $query; // Added this line to get a working solution
 }
}
add_action( \'pre_get_posts\', \'my_breakfast_query\' );

结束

相关推荐