Post Pic

WordPress trick: function to get tags related to category

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

8 Responses

Aug 09 2012 07:55

Great code, thanks!

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

Dec 06 2012 21:38

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.

Dec 12 2012 04:45

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

Jan 16 2013 22:27

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.

Mar 05 2013 04:39

hi, where do I place

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

In a query?

Apr 16 2013 02:46

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.

May 03 2013 06:12

hi, where do I place:

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

???
thanks!

May 09 2013 22:31

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;

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox