admin管理员组文章数量:1333711
This maybe should be posted on a php forum, but since it's WordPress related, I'm giving it a try here..
With this code, I'll get the total count of posts work a certain tag;
$term = get_term_by('slug', lizard, post_tag);
echo $term->count;
How do I write if I would like to know how many posts there are in 10 different tags together?
I guess some kind of array?
This maybe should be posted on a php forum, but since it's WordPress related, I'm giving it a try here..
With this code, I'll get the total count of posts work a certain tag;
$term = get_term_by('slug', lizard, post_tag);
echo $term->count;
How do I write if I would like to know how many posts there are in 10 different tags together?
I guess some kind of array?
Share Improve this question asked Jun 11, 2020 at 19:53 JoBeJoBe 1712 silver badges11 bronze badges1 Answer
Reset to default 1How do I write if I would like to know how many posts there are in 10 different tags together?
If you mean the combined total number of posts, i.e. the sum of the count
value of each term, then:
Yes, you can put the slug list in an array and loop through the items to manually sum the grand total:
$term_slugs = [ 'slug', 'slug-2', 'etc' ]; $post_count = 0; foreach ( $term_slugs as $slug ) { $term = get_term_by( 'slug', $slug, 'post_tag' ); $post_count += $term->count; // This is just for testing. echo $term->name . ': ' . $term->count . '<br>'; } echo 'Grand total posts: ' . $post_count;
Or if all you need is just the grand total, then you can simply use
get_terms()
orget_tags()
(which usesget_terms()
btw) withwp_list_pluck()
without having to do aforeach
loop:$term_slugs = [ 'slug', 'slug-2', 'etc' ]; $terms = get_terms( [ 'taxonomy' => 'post_tag', 'slug' => $term_slugs, ] ); /* Or with get_tags(): $terms = get_tags( [ 'slug' => $term_slugs ] ); */ $post_count = array_sum( wp_list_pluck( $terms, 'count' ) ); echo 'Grand total posts: ' . $post_count;
本文标签: Count several post tags
版权声明:本文标题:Count several post tags 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742355422a2459310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论