WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an existing function or call in which you can pass a category id to determine if the category has child posts?

share|improve this question
up vote 2 down vote accepted

The get_term() function accepts a term id and a taxonomy slug. It returns an object that contains a "count" for that term.

$obj_term = get_term(123, 'your-taxonomy-slug');
echo $term->count;

I'm not aware of any utility function that just counts posts in a particular category.

share|improve this answer
Thanks! I also thought about using $obj= get_posts('category=n'); if($obj) {//has child!}. Is one any more expensive than the other? – Scott B Jan 26 '11 at 0:08
@Scott B - I think get_term() is probably faster. They both run DB queries, but get_posts() does it by initiating a new wp_query object and running the query method, which runs all kinds of logic and seems pretty roundabout for the info you're trying to get. If you want the fastest option, you might look at rolling your own utility function? It'd be pretty trivial to just run a COUNT query, I'd think. I'm actually pretty surprised there isn't a function for this in core! – MathSmath Jan 26 '11 at 0:32
That works perfectly fine. I've got a "Related Posts" list at the bottom of my category landing pages, but since I allow the option of listing empty categories, I'm wrapping the div that holds the link list in this wrapper to check for child posts. – Scott B Jan 26 '11 at 2:49

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.