如何按条款查询自定义帖子类型?

时间:2012-05-03 作者:jilseego

这看起来很琐碎,但我无法让它发挥作用。这是我的代码:

        $args = array(
            \'numberposts\'     => -1,
            \'eventcategory\'   => \'nice-events\',
            \'post_type\'       => \'event\',
            \'post_status\'     => \'publish\' 
        );

        var_dump(get_posts($args));
正如你所见,我的分类法是eventcategory 术语是nice-events. 实际上有50多个术语,无论我用什么术语来代替nice-events 我总是得到同样的结果:所有的帖子。所以这个词被忽视了,我不知道为什么。

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

你不能自己编造论点,而不是替换\'category\' 参数,使用\'tax_query\'.

看见"taxonomy parameters" section of the codex on get_posts.

$args = array(
    \'post_type\' => \'event\',
    \'post_status\' => \'publish\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'eventcategory\',
            \'field\' => \'slug\',
            \'terms\' => \'nice-events\',
        ),
    ),
);


$your_query = get_posts( $args );

// do something with $your_query
或者,您可以使用WP_Query 类别:

$args = array(
    \'posts_per_page\' => -1,
    \'post_type\' => \'event\',
    \'post_status\' => \'publish\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'eventcategory\',
            \'field\' => \'slug\',
            \'terms\' => \'nice-events\'
        ),
    ),
);


$your_query = new WP_Query( $args );

// do something with $your_query

结束

相关推荐