So it just happens that I needed something like that for a project I'm working on. I simply wrote a query to select all posts of a custom type, then I check what are the actual terms of my taxonomy they are using.
Then I got all terms of that taxonomy using get_terms()
and then I only used those that were in both of the lists, wrapped it up in a function and I was done.
But then I needed more then just the ID's: I needed the names so I added a new argument named $fields
so I could tell the function what to return. Then I figured that get_terms
accepts many arguments and my function was limited to simply terms that are being used by a post type so I added one more if
statement and there you go:
The Function:
/* get terms limited to post type
@ $taxonomies - (string|array) (required) The taxonomies to retrieve terms from.
@ $args - (string|array) all Possible Arguments of get_terms http://codex.wordpress.org/Function_Reference/get_terms
@ $post_type - (string|array) of post types to limit the terms to
@ $fields - (string) What to return (default all) accepts ID,name,all,get_terms.
if you want to use get_terms arguments then $fields must be set to 'get_terms'
*/
function get_terms_by_post_type($taxonomies,$args,$post_type,$fields = 'all'){
$args = array(
'post_type' => (array)$post_type,
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
$terms = array();
while ($the_query->have_posts()){
$the_query->the_post();
$curent_terms = wp_get_object_terms( $post->ID, $taxonomy);
foreach ($curent_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$terms[] = $c;
}
}
}
wp_reset_query();
//return array of term objects
if ($fields == "all")
return $terms;
//return array of term ID's
if ($fields == "ID"){
foreach ($terms as $t){
$re[] = $t->term_id;
}
return $re;
}
//return array of term names
if ($fields == "name"){
foreach ($terms as $t){
$re[] = $t->name;
}
return $re;
}
// get terms with get_terms arguments
if ($fields == "get_terms"){
$terms2 = get_terms( $taxonomies, $args );
foreach ($terms as $t){
if (in_array($t,$terms2)){
$re[] = $t;
}
}
return $re;
}
}
Usage:
If you only need a list of term id's then:
$terms = get_terms_by_post_type('tag','','snippet','ID');
If you only need a list of term names then:
$terms = get_terms_by_post_type('tag','','snippet','name');
If you only need a list of term objects then:
$terms = get_terms_by_post_type('tag','','snippet');
And if you need to use extra arguments of get_terms like: orderby, order, hierarchical ...
$args = array('orderby' => 'count', 'order' => 'DESC', 'hide_empty' => 1);
$terms = get_terms_by_post_type('tag',$args,'snippet','get_terms');
Enjoy!
Update:
To fix the term count to specific post type change:
foreach ($current_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$terms[] = $t;
}
}
to:
foreach ($current_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$t->count = 1;
$terms[] = $t;
}else{
$key = array_search($t, $terms);
$terms[$key]->count = $terms[$key]->count + 1;
}
}