Get tags related to category


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. //to go in functions.php
  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. // template tag
  33.  
  34. $args = array('categories' => '12,13,14');
  35. $tags = get_category_tags($args);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.