我想根据特定的自定义帖子类型显示自定义分类的计数。目前,我正在使用get_terms
列出分类标准的所有条款。分类标准由多个邮政类型使用。因此,计数器显示每个帖子类型的分类法的所有用法。
有没有办法限制单个帖子类型的计数?
这是我的实际代码:
get_terms(array(
'taxonomy' => 'tax',
'hide_empty' => true,
'orderby' => 'count',
'order' => 'DESC',
'number' => '10',
));
在foreach
内部,我使用term->count
来显示使用计数器。
我不建议使用get_terms
,因为这会返回分类法的所有条款,而不是所有与帖子相关的条款。
替代解决方案是使用get_posts
,在这里阅读更多https://developer.wordpress.org/reference/functions/get_posts/
$my_posts = get_posts(array(
'post_type' => 'post', //post type
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'tax', //taxonomy name
'field' => 'id', //field to get
'terms' => 1, //term id
)
)
));
然后你可以统计返回的帖子数量:
$count = count($my_posts);
我认为以下链接更有助于更好地理解。 Link
这是为您服务的代码。
$args = array(
'post_type' => 'Your_custom_post_type',//Your custom post type here.
'tax_query' => array(
array(
'taxonomy' => 'Your_taxonomy',//Your taxonomy is here.
'field' => 'slug',
)
)
);
现在我们print_r
$args
更好地理解我们得到了什么。
_e('<pre>');
print_r($args);
_e('</pre>');
只是得到你的疑问
$your_query = new WP_Query( $args );