I have a project that requires me to list out the available terms for each custom post type and indicate visually which of the terms/categories are empty via css/javascript. Is there a way to return a list of terms/categories and say add a class to the empty ones ? Thanks for any and all assistance.
2 comments
Comments are closed.
Yes there is. First you get your terms using get_terms() (I’m assuming your cpt has associated taxonomy with it)
This should do it.
EDIT
After
$custom_terms
variable do aprint_r($custom_terms);
to see what the variable holds. You should get an array filled withstdClass Object
s, one for each category in this taxonomy.So you can further do something like this:
This will show you names of non empty categories in your taxonomy.
Here is how to query the terms with the WP_Term_Query class (to complement dingo_d’s answer):
Then, this would be how to iterate over the terms:
This is a very short and concise article on why you should now use WP_Term_Query:
https://medium.com/vunamhung/say-goodbye-to-get-terms-use-wp-term-query-db774df6d9ea