我可以很好地为我的自定义分类法获取类别id或slug,但我需要能够将所有帖子作为该分类法条目的数组获取。我的代码如下:
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
endwhile;
当我将“category\\u name”=>“my\\u taxonomy\\u name”添加到args数组时,它只会导致$the\\u查询为空,尽管我知道其中有post。我还尝试将其更改为“cat”=>22,但这也有相同的错误。
有人能帮忙吗?
干杯约翰
最合适的回答,由SO网友:Evan Yeung 整理而成
Check out the Taxonomy Parameters.
<?php
$args = array(
\'post_type\' => \'product\',
\'post_status\' => \'publish\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'taxonomy_name\',
\'field\' => \'id\',
\'terms\' => \'22\'
)
)
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
//content
endwhile;
?>