I've been out looking for a snippet that will let me organize tags under a certain category and count how many times that tag has been used. Example:

Category name: Photography

  • Plants (3)
  • Animals (14)
  • People (4)
  • Vehicles (2)

So far what I have is under a custom query and it's as follows:

<h3>Identifying Terms Used</h3>
    <?php $photographyTags = new WP_Query(array('category_name' => 'photography')); ?>
    <?php if($photographyTags->have_posts()) : while($photographyTags->have_posts()) : $photographyTags->the_post(); ?>
        if(get_the_tag_list()) {
            echo get_the_tag_list('<li>','</li><li>','</li>');
    <?php endwhile; endif; ?>

There's a unique issue with this bit of code where when I have a tag that has been used more than once, the name of the tag repeats itself as many times as it's been used. So when I have a tag name like Mesquite Tree and I've used it 4 times, I'll see the word Mesquite Tree repeated 4 times in my list.

Any help would be hugely appreciated!

  • If anyone is still available who knows the answer to this, I would love to hear from you! I still haven't found what I'm looking for... Commented Apr 3, 2018 at 1:33
  • Still trying to find an answer to this question if anyone out there has one. Commented Apr 6, 2018 at 12:24

1 Answer 1


I finally found an answer. It didn't work the first few times I used it, but after reworking some HTML and with a better grasp of what I'm doing, I was able to put this together. This was from an answer here on W.SE but I can't locate it anymore:

    $custom_query = new WP_Query( array( 'cat' => get_query_var('cat') ) );

    if ( $custom_query->have_posts() ) :
       while ( $custom_query->have_posts() ) : $custom_query->the_post();
          $posttags = get_the_tags();
          if ( $posttags ) {
             foreach( $posttags as $tag ) {
                $all_tags[] = $tag->term_id;

    $tags_arr = array_unique($all_tags); $tags_str = implode(",", $tags_arr);

    $args = array(
        'smallest'  => 14,      'largest'    => 14,
        'unit'      => 'px',    'number'     => 0,
        'format'    => 'list',  'show_count' => 1,
        'include'   => $tags_str

What I'm able to gather from this is that we need to use a seperate loop and invoke a new WP_Query.

This query is using an array to extract the category that was clicked. Once that happens, the tags listed under that category are gathered up and laid out in a tag cloud.

Then we're reworking the tag cloud to display in a list instead of the typical cloud format. I added show_count => 1 to show how many times a particular tag was used.

I will post the page on my website that I used it so you can see it in action. Of course, I won't do that until it's done :)

  • NOTICE: if you do not have any tags under the category, it will parse an error until you add a tag. It took me awhile to figure this out. So the error I got was this: Warning: array_unique() expects parameter 1 to be array, null given in C:\xampp\htdocs\wordpress\wp-content\themes\harshclimate\category-photography.php What it's saying here is that NULL (which means nothing basically) was used instead of a value in an array. So if you don't add a tag, it'll return nothing (NULL) and you'll get a warning :) Commented Apr 7, 2018 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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