我已经试过这个代码了:
$cats = get_terms('category');
$counts_by_term = wp_list_pluck( $cats, 'count', 'slug' );
echo $counts_by_term['uncategorized'];
但问题是,分类分类法对每个帖子类型都是共享的。
:
我有一个名为"推荐人"的CPT,启用了该类别,并在那里创建了一个未分类类别的帖子。我也有5个帖子在帖子类型,所有未分类的类别分配给他们。
现在,我只想从推荐帖子类型中获得未分类类别的帖子数量。
上面的代码将输出:6应该是:1
这是我的解决方案,
$cat_id = 1;
$post_type = 'testimonials';
$args = array(
'numberposts' => -1,
'category' => $cat_id,
'post_type' => $post_type,
);
$count_posts = get_posts( $args );
$total_posts = count($count_posts);
echo $total_posts;
//这样可以减少对数据库的压力
$cat_id = 1;
$args = array(
'category'=>$cat_id,
'post_type'=>'testimonials',
);
$postTypes = new WP_Query($args);
$numberOfPosts = $postTypes->found_posts;
echo $numberOfPosts;
$cats = get_terms('category', array('hide_empty' => false, 'show_count' => 1, 'orderby'=>'id', 'order' => 'ASC' )); //in array args put what you want
foreach($cats as $cat):
echo $cat->count; // return total posts in your custom post type category with 'show_count' => 1,
endforeach;