[wp-trac] [WordPress Trac] #18106: get_terms of posts in a specific post_type

WordPress Trac wp-trac at lists.automattic.com
Tue Jul 26 11:01:10 UTC 2011


#18106: get_terms of posts in a specific post_type
-----------------------------+------------------------------
 Reporter:  braydonf         |       Owner:
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Taxonomy         |     Version:
 Severity:  normal           |  Resolution:
 Keywords:                   |
-----------------------------+------------------------------

Comment (by braydonf):

 Here is SQL to achieve something similar:

 {{{

     static public function get_terms_by_post_type( $taxonomies,
 $post_types ) {

         global $wpdb;

         $query = $wpdb->prepare( "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('".join( "', '", $post_types )."') AND tt.taxonomy
 IN('".join( "', '", $taxonomies )."') GROUP BY t.term_id");

         $results = $wpdb->get_results( $query );

         return $results;

     }

 }}}


 Perhaps better for another function all together.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/18106#comment:1>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list