DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Do you ever wanted to be able to get tags related to one (or more) specific category? If yes, I’m pretty sure you’ll be delighted with this very cool tip.

First, here is the function you have to paste in your function.php file:

function get_category_tags($args) {
	global $wpdb;
	$tags = $wpdb->get_results
	("
		SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
		FROM
			wp_posts as p1
			LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
			LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
			LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,

			wp_posts as p2
			LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
			LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
			LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
		WHERE
			t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
			t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
			AND p1.ID = p2.ID
		ORDER by tag_name
	");
	$count = 0;
	foreach ($tags as $tag) {
		$tags[$count]->tag_link = get_tag_link($tag->tag_id);
		$count++;
	}
	return $tags;
}

Once you have pasted the function, you can use it in your theme:

$args = array('categories' => '12,13,14');
$tags = get_category_tags($args);

Credits: WordPress Codex

9 Comments

  1. Great code, thanks!

    Can you give some advice how to get a custom taxonomy instead of the tags listed?

  2. Great article!! Saved me at least an hour! 🙂 I was looking for something to get all custom taxonomies related to the current category. This was very useful!

    @Ben, Just replace post_tag in the SQL query with “.$args[‘taxonomy’].”. Then you can use something like $args = array(‘categories’ => ‘12,13,14’, ‘taxonomy’ => ‘taxonomyname’); to find taxonomy terms related to the category. Haven’t tested yet, but has to work.

  3. I’m not sure I follow just how the tags are related to the category? That’s not something you can set up somewhere.

  4. Awesome code. HUGE help.

    @Kathy tags and cats get related when you have a post in a category with tags. Good practice is to have one cat per post and as many tags as required.

    This way you can show a tag archive of only posts that have a specific category.

    By default tag archives will show all posts with a specified tag regardless of the categories those posts are in.

  5. hi, where do I place

    $args = array(‘categories’ => ‘12,13,14’);
    $tags = get_category_tags($args);

    In a query?

  6. I’m wanting to do the same thing, left column lists all the categories and the right side shows the products. But I want to add a right sidebar that displays the tags related to each specific category I’m in.

    Is this what the example can do? Also where does the second part of the code go as it hasn’t been answered yet.

    Thanks.

  7. hi, where do I place:

    $args = array(‘categories’ => ’12,13,14′);
    $tags = get_category_tags($args);

    ???
    thanks!

  8. Here is a function that actually works

    function get_category_tags($args) {
    global $wpdb;
    $tags = $wpdb->get_results("
    SELECT DISTINCT
    terms2.term_id as tag_ID,
    terms2.name as tag_name,
    t2.count as posts_with_tag
    FROM
    $wpdb->posts as p1
    LEFT JOIN $wpdb->term_relationships as r1 ON p1.ID = r1.object_ID
    LEFT JOIN $wpdb->term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms1 ON t1.term_id = terms1.term_id,

    $wpdb->posts as p2
    LEFT JOIN $wpdb->term_relationships as r2 ON p2.ID = r2.object_ID
    LEFT JOIN $wpdb->term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
    LEFT JOIN $wpdb->terms as terms2 ON t2.term_id = terms2.term_id
    WHERE (
    t1.taxonomy = 'category' AND
    p1.post_status = 'publish' AND
    terms1.term_id = '".$args['categories']."' AND
    t2.taxonomy = 'post_tag' AND
    p2.post_status = 'publish' AND
    p1.ID = p2.ID
    )
    ");
    $count = 0;
    foreach ($tags as $tag) {
    $tags[$count]->tag_link = get_tag_link($tag->tag_id);
    $count++;
    }
    return $tags;
    }


    Use it as such:

    $args = array('categories' => '6');
    $tags = get_category_tags($args);

    $content .= "";
    foreach ($tags as $tag) {
    $content .= "tag_link">$tag->tag_name";
    }
    $content .= "";
    echo $content;

  9. Hi – this is ace, but is there any way to filter out a couple of specific tags from the result?

Leave a Comment

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