Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Get Terms used by Post Type
  5. * Fetches only terms actually used by posts of a specified post type
  6. *
  7. * @param string $taxonomy the taxonomy to look for terms
  8. * @param string $post_type the post type to match the taxonomy terms found
  9. *
  10. * @return array the query result (an array of taxonomy terms as objects)
  11. */
  12. function get_terms_by_post_type( $taxonomy, $post_type ) {
  13.  
  14. global $wpdb;
  15. $query = $wpdb->get_results(
  16. "SELECT t.*, COUNT(*) from $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id WHERE p.post_type IN('" . $post_type . "') AND tt.taxonomy IN('" . $taxonomy . "') GROUP BY t.term_id"
  17. );
  18.  
  19. return $query;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement