显示来自多个自定义发布类型的自定义分类

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

我有多个自定义帖子类型,每个都有多个类别。我试图用这段代码显示所有自定义帖子类型+常规帖子中的某些类别。

<?php query_posts( array(
 \'post_type\' => array( \'post\', \'miss_behave\',\'emily_davies\', \'gemma_patel\', \'poppy_smythe\' ),
 \'tax_query\' => array(
    \'relation\' => \'AND\',
    array(
        \'taxonomy\' => \'category\',
        \'field\' => \'term_id\',
        \'terms\' => 4
    ),
    array(
        \'taxonomy\' => \'miss_behave_category\',
        \'field\' => \'term_id\',
        \'terms\' => 141
    ),
    array(
        \'taxonomy\' => \'emily_category\',
        \'field\' => \'term_id\',
        \'terms\' => 142
    ),
    array(
        \'taxonomy\' => \'gemma_category\',
        \'field\' => \'term_id\',
        \'terms\' => 143
    ),
    array(
        \'taxonomy\' => \'poppy_category\',
        \'field\' => \'term_id\',
        \'terms\' => 144
    )


),
 \'showposts\' => 4)
 ); if (have_posts()) : ?>
这样页面就不会显示任何内容。仅当我仅查询默认帖子时,页面才会显示一些内容。

请帮忙!

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

我想出来了。将“关系”设置为“或”很重要。

<?php $myquery[\'tax_query\'] = array(
\'relation\' => \'OR\',
array(
    \'taxonomy\' => \'category\',
    \'terms\' => array(\'home-slides\'),
    \'field\' => \'slug\',
),
array(
    \'taxonomy\' => \'miss_behave_category\',
    \'terms\' => array(\'home-slides-1\'),
    \'field\' => \'slug\',
),
array(
    \'taxonomy\' => \'emily_category\',
    \'terms\' => array(\'home-slides-2\'),
    \'field\' => \'slug\',
),
);
query_posts($myquery);
 ?>

结束

相关推荐