'Is it possible in WordPress to test for an empty term or category?
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.
Solution 1:[1]
Here is how to query the terms with the WP_Term_Query class (to complement dingo_d's answer):
$args = array(
'taxonomy' => 'my_taxonomy',
'hide_empty' => false
);
$custom_terms = new WP_Term_Query($args);
Then, this would be how to iterate over the terms:
foreach($custom_terms->terms as $term){
if ($term->count != 0) {
print_r($term->name);
}
}
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Abdiel León |
