Posted By

johnandrewsroot on 02/12/10


Tagged

get wordpress category tags


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

marclarr
booski


Wordpress get_category_tags


 / Published in: PHP
 

URL: http://wordpress.org/support/topic/276635?replies=26

Verified working for WP v2.9.1

  1. // In your theme's functions.php insert the following function:
  2.  
  3. function get_category_tags($args) {
  4. global $wpdb;
  5. $tags = $wpdb->get_results
  6. ("
  7. SELECT DISTINCT terms2.term_id as tag_id, terms2.name as tag_name, null as tag_link
  8. FROM
  9. wp_posts as p1
  10. LEFT JOIN wp_term_relationships as r1 ON p1.ID = r1.object_ID
  11. LEFT JOIN wp_term_taxonomy as t1 ON r1.term_taxonomy_id = t1.term_taxonomy_id
  12. LEFT JOIN wp_terms as terms1 ON t1.term_id = terms1.term_id,
  13.  
  14. wp_posts as p2
  15. LEFT JOIN wp_term_relationships as r2 ON p2.ID = r2.object_ID
  16. LEFT JOIN wp_term_taxonomy as t2 ON r2.term_taxonomy_id = t2.term_taxonomy_id
  17. LEFT JOIN wp_terms as terms2 ON t2.term_id = terms2.term_id
  18. WHERE
  19. t1.taxonomy = 'category' AND p1.post_status = 'publish' AND terms1.term_id IN (".$args['categories'].") AND
  20. t2.taxonomy = 'post_tag' AND p2.post_status = 'publish'
  21. AND p1.ID = p2.ID
  22. ORDER by tag_name
  23. ");
  24. $count = 0;
  25. foreach ($tags as $tag) {
  26. $tags[$count]->tag_link = get_tag_link($tag->tag_id);
  27. $count++;
  28. }
  29. return $tags;
  30. }
  31.  
  32. // In your theme document call the function as follows. Notice it accepts multiple category id's:
  33.  
  34. $args = array(
  35. 'categories' => '12,13,14'
  36. );
  37. $tags = get_category_tags($args);
  38.  
  39. // This will return an array that you could do the following with:
  40.  
  41. $content .= "<ul>";
  42. foreach ($tags as $tag) {
  43. $content .= "<li><a href=\"$tag->tag_link\">$tag->tag_name</a></li>";
  44. }
  45. $content .= "</ul>";
  46. echo $content;

Report this snippet  

You need to login to post a comment.