Exclude Custom Taxonomies

时间:2014-04-25 作者:Jaeeun Lee

我试图显示来自常规帖子部分和自定义帖子类型的最新帖子,从每个自定义帖子类型中排除一个自定义分类术语。但是最近的帖子部分没有显示任何内容。

   <?php 
    // the query
    $the_query = new WP_Query(
     array(
    \'post_type\' => array( \'post\', \'miss_behave\', \'emily_davies\',\'gemma_patel\',\'poppy_smythe\' ),
    \'category__not_in\' => 4,
    \'tax_query\'          => array(
    \'taxonomy\' => array(\'miss_behave_category\',\'emily_category\',\'gemma_category\',\'poppy_category\'),
    \'terms\' => array(141,142,143,144),      
    \'field\' => \'id\',
    \'operator\' => \'NOT IN\' 
        )
        )
    );?>

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

你的tax_query 不正确。taxonomy 不接受数组。

taxonomy (string) - Taxonomy.

http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
你需要重写你的论点the following from the Codex:

$args = array(
    \'post_type\' => \'post\',
    \'tax_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'taxonomy\' => \'movie_genre\',
            \'field\' => \'slug\',
            \'terms\' => array( \'action\', \'comedy\' )
        ),
        array(
            \'taxonomy\' => \'actor\',
            \'field\' => \'id\',
            \'terms\' => array( 103, 115, 206 ),
            \'operator\' => \'NOT IN\'
        )
    )
);
$query = new WP_Query( $args );
您可以稍微自动化一下:

$taxonomies = array(\'miss_behave_category\',\'emily_category\',\'gemma_category\',\'poppy_category\');

$args = array(
  \'post_type\' => array( \'post\', \'miss_behave\', \'emily_davies\',\'gemma_patel\',\'poppy_smythe\' ),
  \'category__not_in\' => 4
);

$args[\'tax_query\'][\'relation\'] = \'OR\';
foreach ($taxonomies as $tax) {
  $args[\'tax_query\'][] = array(
    \'taxonomy\' => $tax,
    \'terms\' => array(141,142,143,144),      
    \'field\' => \'id\',
    \'operator\' => \'NOT IN\' 
  );
}
// var_dump($args);

$the_query = new WP_Query($args);
然而,“类别”是一种分类法,所以您也可以将其与其他分类法以及所有这些分类法一起包括在内NOT INs不太可能非常有效。我会仔细考虑这些需要包括在哪里。

结束

相关推荐