Skip to content

Instantly share code, notes, and snippets.

@stefanolaru
Created October 25, 2014 10:00
Show Gist options
  • Save stefanolaru/3959ef637b7835046736 to your computer and use it in GitHub Desktop.
Save stefanolaru/3959ef637b7835046736 to your computer and use it in GitHub Desktop.
Wordpress - get terms by custom post type
function get_terms_by_post_type($taxonomy, $post_type) {
//
global $wpdb;
$query = $wpdb->prepare("SELECT t4.*, t3.term_taxonomy_id, t3.description, COUNT(t2.ID) as `count` FROM wp_term_relationships AS t1 LEFT JOIN wp_posts AS t2 ON t1.object_id=t2.ID LEFT JOIN wp_term_taxonomy AS t3 ON t1.term_taxonomy_id=t3.term_taxonomy_id LEFT JOIN wp_terms AS t4 ON t3.term_id=t4.term_id WHERE t2.post_type='".$post_type."' AND t3.taxonomy='".$taxonomy."' GROUP BY t4.term_id", array());
$results = $wpdb->get_results($query);
//
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment