Show the amount of posts in a tag in a specific category has

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:

Apples(5)

As of now it just shows Apples

Here is my code I want to modify:

    <?php
      if (is_category()){
           $cat = get_query_var('cat');
           $yourcat = get_category ($cat);
       }
       $tag_IDs = array();
           query_posts('category_name=health');
                   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;
                                   endif;
                       }
                       endif;
                       endwhile; endif;
                   wp_reset_query();
       
               echo "<ul>";
               foreach($tag_IDs as $tag_ID){
               echo '<a href="'.get_tag_link($tag_ID).'">'.$tag_names[$tag_ID].'</a>';
               }
           echo "</ul>";
       ?>                                  

Show the amount of posts in a tag in a specific category has
Show the amount of posts in a tag in a specific category has

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

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.

<?php
                            $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;
                                          $all_tags[$tag->term_id]['count']++;
                                        }
                                    }
                                endwhile;
                            endif;
    
                            $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>';
                            }
                            wp_reset_query()
                         ?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x