TAX_QUERY不会产生任何结果

时间:2020-08-13 作者:Cain Nuke

我想显示自定义分类类别中的自定义类型帖子,所以我这样做了:

$news = get_posts(
array(
    \'posts_per_page\' => -1,
    \'post_type\' => \'news\',
    \'tax_query\' => array(
            \'taxonomy\' => \'news_category\',
            \'field\' => \'term_id\',
            \'terms\' => 82,
        ),
    )
); 
$postslist = get_posts( $news );    

foreach ($postslist as $crap) { ?>
     <a href="<?php $crap->title; ?>"><?php $crap->title; ?></a>
<?php }?>
我从中什么也得不到。为什么会这样?我仔细检查了类别ID及其正确性。此外,我还使用自定义帖子类型插件来注册自定义分类法。

1 个回复
SO网友:Howdy_McGee

查看WP_Query documentation on tax_queries 我们可以看到它接受嵌套数组作为参数。将其与提供的代码进行比较:

\'tax_query\' => array(
    \'taxonomy\' => \'news_category\',
    \'field\' => \'term_id\',
    \'terms\' => 82,
),
应如下所示:

\'tax_query\' => [
    [
        \'taxonomy\' => \'news_category\',
        \'field\' => \'term_id\',
        \'terms\' => 82,
    ]
],

相关推荐