Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.