[英]Show count of Custom Taxonomy only for a specific Custom Post Type
我想根据特定的自定义帖子类型显示自定义分类的计数。 目前,我正在使用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);
我认为以下链接更有助于更好地理解。 链接
这是为您服务的代码。
$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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.