WordPress: Get Tags for a Specific Category

WordPress is such a powerful tool out of the box that it is surprising to find something that it doesn’t have a function for. Here is a code snippet that will allow you to show tags that are related to a specific category. The nice thing about this snippet is that it does not require any complicated custom DB queries but uses WordPress’ built in features.

It is as simple as pasting this code wherever you would like to display tags for a specific category. If you would like to change the appearance of the tags, take a look at the options available for wp_tag_cloud(). Don’t forget to change the category name on line 1.

<?php
$custom_query = new WP_Query( 'posts_per_page=-1&category_name=blog' );
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;
			}
		}
	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
);
wp_tag_cloud( $args );
?>

16 thoughts on “WordPress: Get Tags for a Specific Category”

  1. Excellent, thank you for this.

    I am using it on a WordPress ‘page’ and it’s working perfect. One question I do have though. Is there a way to have it so that shows in two lists rather than one long one?

    Thank you for any help with this.

  2. Excellent one!!!! But what code should I place instead of ‘category_name=blog’ if I’m in the current category? Is it possible? Thank U!

      1. I think AlexPop meant:
        How can the current category be put inside the category_name? Meaning if I am actually in the category “News” how does the code need to look to display the tags in the current category “News”. Making it variable. One code for all categories? Something like: “.$mycatid.”
        I’ve tried for hours, but couldnt figure it out. Would be nice, if you could help!

        1. Anita, thanks for making it clear for me. Add this code to the beginning of the code in this tutorial:

          if ( is_category() ) {
          $cat = get_category( get_query_var( 'cat' ), false );
          $thecat = $cat->slug;
          } else {
          $thecat = '';
          }

          1. Hi Josh, I have been trying to get this little change to work but I just can’t…I can edit code and figure things out through trial and error but I don’t actually know how to write code so I’m not sure what is wrong. Could you please post an example of the whole code?

            I understand that this assigns the text from the database under category to $thecat but then how do you use that? Basically I want to display the tags per category but only on some categories. The original code works if I add a if in_category check before it but to do it in more than one category means I have to paste that code in over and over for every category. So I am just trying to get the current category and then insert it automatically into new WP_Query(‘posts_per_page=-1&category_name=blog’)

            I think that’s the right logic anyway lol please correct me if I’m on the wrong track…

            Anyway any help you can be would be greatly appreciated. I have successfully wasted about 4 hours on this thus far (why isn’t this native? ugh!). Using WordPress 3.0

  3. This is exactly what I needed. Thank you so much Josh.
    Is there any way to limit how many tags to show?
    I’m using it on my site and I’m having a little problem. I have many (possibly 20-30) tags in some categories and that’s why the tag list is getting too long.
    It would be great if you add this feature too.

  4. Josh, sorry to disturb you again…
    Can I show the post count next to the tags? like – Photoshop (15 posts)
    Currently it shows only when hovering the mouse pointer.

  5. Thanks for this cool script. I have a long list of tags which makes the page so long. How to get the tags in drop down?
    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *