I have a custom post type “Services”. It has a hierarchal taxonomy of “service-category”. Service-category has both parent and child terms.
I need to count the number of parent terms in the taxonomy. NOT posts. The # of terms in the taxonomy is being used in my code to layout the page in a recursive loop.
Everything I have researched is about the number of posts per term, not the number of terms in the taxonomy.. I am sure I am missing an obvious way of doing this, but I haven’t run across it.
Thanks in advance for your help!
As @shanebp suggests, you could use wp_count_terms()
like this:
$numTerms = wp_count_terms( 'service-category', array(
'hide_empty'=> false,
'parent' => 0
) );
The above will list All top parent terms, empty or not. This function uses get_terms()
functions arguments which can be found in the link or the arguments below:
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'number' => '',
'fields' => 'all',
'slug' => '',
'parent' => '',
'hierarchical' => true,
'child_of' => 0,
'get' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'offset' => '',
'search' => '',
'cache_domain' => 'core'
);