I am creating a page template in Wordpress that displays multiple tags that are in a particular category. I have this working, but now I want to have the number of posts within each tag that is displayed as well like if I had a tag called apples with 5 posts it would look like this:


As of now it just shows Apples

Here is my code I want to modify:

      if (is_category()){
           $cat = get_query_var('cat');
           $yourcat = get_category ($cat);
       $tag_IDs = array();
                   if (have_posts()) : while (have_posts()) : the_post();
                       $posttags = get_the_tags();
                           if ($posttags):
                           foreach($posttags as $tag) {
                               if (!in_array($tag->term_id , $tag_IDs)):
                                   $tag_IDs[] = $tag->term_id;
                                   $tag_names[$tag->term_id] = $tag->name;
                       endwhile; endif;
               echo "<ul>";
               foreach($tag_IDs as $tag_ID){
               echo '<a href="'.get_tag_link($tag_ID).'">'.$tag_names[$tag_ID].'</a>';
           echo "</ul>";

enter image description here enter image description here

  • 1
    I see you've re-posted this question several times and then deleted it and reposted it. You shouldn't do that, you'll get flagged by the automated system for suspicious behaviour. It'll also put off people posting answers. Stop it. If nobody answers your question, edit it to make it more attractive, don't spam the site – Tom J Nowell Sep 13 '20 at 21:30
function wp_get_postcount($id)
    $contador = 0;
    $taxonomia = 'categoria';
    $args = array(
        'child_of' => $id,
    $tax_terms = get_terms($taxonomia,$args);
    foreach ($tax_terms as $tax_term){
        $contador +=$tax_term->contador;
    return $contador;
  • Can you edit your answer to explain how this answers the OP's question? – Tom J Nowell Sep 13 '20 at 21:32

The reason you get this is because you're using query_posts and looping over posts, not terms/categories.

So while you want this:

terms = child terms of health
for each term in terms
    print the term name
    print the number of posts in that term

What the code actually does is this:

posts = all posts that have the category health
for each posts
    print a list of tags this post has

query_posts, aside from being a function you should never use, fetches posts, not terms/categories. It does not make sense to fetch posts here.

So instead we need to do this:

healthterm = the health term
terms = child terms of health term
for each term in terms
    print the name
    print the post count

So first we get the health term:

$parent = get_term_by( 'slug', 'health', 'category' );

Then get its children with something similar to this:

$terms = get_terms( 'category', [
    'parent' => $parent->term_id
] );

And finally, loop over the terms and print their names/counts

if ( !is_wp_error( $terms ) && !empty( $terms ) ) {
    foreach ( $terms as $term ) {
        // ...
        echo $term->name . ' ' .$term->count;

These are the missing building blocks you need to achieve your goal.

  • This did not work. It just spit out how many posts I have in each category. – RLM Oct 13 '20 at 22:11
  • But that's the title of your question, this answer gets the health tag and prints out the post count of its child terms. If that's not what you wanted then you need to update your question to ask what you wanted. Unless you're looking for a copy paste solution that requires no understanding, in which case I cannot provide that. – Tom J Nowell Oct 14 '20 at 15:14
  • been studying up and using your example. I understand what you are doing in terms of logic and syntax, however nothing is rendered. I've changed the 1st and 2nd parameters get_term_by to id or slug instead of name. Is there something I'm missing? Here is what I have: <?php $parent = get_term_by('slug', 'health', 'category'); $terms = get_terms('category', [ 'parent' => $parent->term_id ]); if(!is_wp_error($terms) && !empty($terms)) { foreach($terms as $term) { echo $term->name. ' ' .$term->count; } } ?> – RLM Nov 1 '20 at 20:10
  • that looks good, I can only assume that either get_terms returned an error object or an empty array, or, that get_term_by failed. An else case on the if statement will help diagnose the first case, but really you need to inspect the variables and double check they contain what you think they contain. Why guess if $terms is an error object, or empty when you can look and see for yourself? Likewise with $parent – Tom J Nowell Nov 2 '20 at 0:10
  • All trouble shooting points to empty, do I need to do some sort of registration of the terms aka get_terms_by? – RLM Nov 2 '20 at 0:25

Here is the code I rewrote that solved my problem. It loops through a category and shows how many articles all the tags inside the category has.

                            $custom_query = new WP_Query( 'posts_per_page=-1&category_name=health' );
                            if ( $custom_query->have_posts() ) :
                                $all_tags = array();
                                while ( $custom_query->have_posts() ) : $custom_query->the_post();
                                    $posttags = get_the_tags();
                                    if ( $posttags ) {
                                        foreach($posttags as $tag) {
                                          $all_tags[$tag->term_id]['id'] = $tag->term_id;
                                          $all_tags[$tag->term_id]['name'] = $tag->name;
                            $tags_arr = array_unique( $all_tags );
                            $tags_str = implode( ",", $tags_arr );
                            $args = array(
                                'smallest'  => 12,
                                'largest'   => 12,
                                'unit'      => 'px',
                                'number'    => 0,
                                'format'    => 'list',
                                'include'   => $tags_str
                            foreach ( $all_tags as $tag_ID => $tag ) {
                                echo '<a href="'.get_tag_link($tag_ID).'">' . $tag['name'] . '</a> ('; echo $tag['count'] . ')<br>';
  • How does this fix the issue? It's just a code block with no explanation :( – Tom J Nowell Dec 3 '20 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.