Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Get Terms used by Post Type
- * Fetches only terms actually used by posts of a specified post type
- *
- * @param string $taxonomy the taxonomy to look for terms
- * @param string $post_type the post type to match the taxonomy terms found
- *
- * @return array the query result (an array of taxonomy terms as objects)
- */
- function get_terms_by_post_type( $taxonomy, $post_type ) {
- global $wpdb;
- $query = $wpdb->get_results(
- "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"
- );
- return $query;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement