How 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;