I have a Custom Post Type that uses the default category, the same as the posts, and some other post types in my site. How can I list all non-empty categories from the custom post type?
For instance, if I have categories “Cat 1”, “Cat 2" and “Cat 3” and I have a default post with a category of “Cat 1", and a custom post with categories “Cat 2” and “Cat 3". How would I write a function that would return “Cat 2” and “Cat 3"?
It seems like the get_terms()
functions don’t support a post_type
argument. Is there a way I can add some other query to a get_terms()
function?
I know that it is possible to hide unused terms:
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true, <-----
) );
However this hides empty for all post types, I'd like to hide empty for just a custom post type.
hide_empty
is based on thecount
column ofwp_terms
. This count is based on the total number of posts of any post type for that term. The number of posts of each post type is not stored separately for each post type, so you can't do what you're asking for withget_terms()
. You would need to write your own query, but it would be very inefficient since you'd essentially need to check every post. You're probably better of registering a second separate taxonomy for your post type.