[英]WordPress. How to get multiple custom post_type total posts count in taxonomy.php?
[英]Wordpress. How to get the custom post_type count in taxonomy.php page
在我的 WordPress v5.7 中,我有自定义分类法和自定义 post_type(歌曲、诗歌)。 在taxonomy.php
模板中,我想要每个 post_type 帖子的计数。
我有下面的代码,它给了我查询的 object / term 的总数:
// current term
$term = get_queried_object();
// total posts from the queried term
$get_posts = get_term($term->term_id, $term->taxonomy);
// total posts count in current term
$total_post_count = $get_posts->count;
如何获取当前学期的歌曲总数和诗歌总数?
您可以使用WP_Query和tax_query 。 检查下面的代码。
$term = get_queried_object();
$args = array(
'post_type' => 'your-post-type-name',
'post_status' => 'publish', // get only publish posts
'posts_per_page' => -1, // get all posts
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'your-taxonomy-name',
'field' => 'term_id',
'terms' => $term->term_id
)
)
);
$AllPostByTerms = new WP_Query( $args );
echo $AllpostByTerms->post_count;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.