Getting the number of posts per category in WordPress

What I would like to be able to do is ask the MySQL if a category has any posts in it or if it is empty. So I can do one thing if there are posts there and another if that category is empty. And try as I might, no matter how hard I beat my head against it, all I get is a bruised head. And, yes, I have to do this conditional because WP is flawed and will break if I try and do certain things with empty categories…..

And the worst part of this, is – I know this is trivial so I hate to even ask, but I guess I don’t hate enough not to ask :) I asked the “community” and got told that there is not a WP function for this ( um, yeah, thanks…. ) that I shouldn’t make straight db calls ( arrrggg ), and that there are plugins that accomplish similar things…. But no real help, so I decided to ask a ninja….

So we want to get the number of posts per category. JTk, you’re in luck! A quick look at the schema (if that diagram is current) tells me we could query the wp_post2cat table and do a COUNT and a GROUP BY query on the category of interest. Even better, if wp_categories.category_count is what I think it is, that’s a simpler query. But let’s see what the code gives us already. In category.php, there are some tasty sounding functions like get_categories(), get_category(), get_category_by_path(), get_category_by_slug() and so on. I installed a clean version of WordPress, added some categories and assigned some fake posts to them. Then I tried some of these functions to see what’s up.

OK, so it sounds like the person who told JTk there were no built-in WordPress functions for this was lying :) This function tells us exactly what we need. A couple of interesting observations, though. I had a category named “Empty Category” with no posts, which does not show up here. The documentation page tells me that I can pass a parameter to include the empty ones easily enough:

Sure enough,there is the empty category. I also noticed that the article count appears in both ->count and ->category_count. I’ll just assume the cached version, ->category_count, will remain correct through the code, but I’m just guessing it doesn’t matter which member we use.

So, that’s fine if we want the whole collection, but what about individual categories? Fortunately, we can extract this same data for both the category ID as well as the name of the category. Here’s an example of each, querying my “First Category” category:

That’s all there is to it! There’s definitely a built in function to inspect categories, and even some nice examples to go with. Happy WordPress hacking, and don’t forget that now’s the time to upgrade to PHP 5 if you haven’t already :)

Like this:

LikeLoading...

Related

About John Herren

John Herren is a developer and technical consultant with focus on web applications. He currently serves as Director of Development for Primetime US, the company behind the hit movie and book The Secet. John was formerly staff writer and developer community evangelist for Zend Technologies. Along with founding neat experiments like TagCloud.com, John is an active member in the mashup community, working with API providers and speaking at conferences. He is a published author of Linux certification study material. John enjoys using open source software like PHP and Ruby on Rails to bend the web into exciting new chimeras of hyperlinked goodness.
View all posts by John Herren

This entry was posted on Wednesday, August 13th, 2008 at 1:32 pm and posted in wordpress. You can follow any responses to this entry through the RSS 2.0 feed.

All I need now is the number of the posts to be able to do this math. When I tried out a few things, I never could get just that number I wanted. I’m not an advanced coder, so I hope I explained it well.

Hi John, this worked great for me. Thanks so much. I was wondering though – the way you are able to extract the “count” for categories with either “->category_count” or “->count”, is there an API that lists these functions? I know the ones used are in examples in those documentations, but I couldn’t really find much else.

Hi, I used the following codes to list all the categories that I have on my site, but the issue is that I have too many categories (100+). So, I would like to paginate the category listing. (i.e 10 categories each page… etc) I don’t know how I would approach this using the get_categories function within WordPress. Help??? Thanks!

Thanks for the post, give me the know about category templating. Currently each category shows 10 post I know I can change this via admin panel. Is there any where in the code I can add how many post it should display on the page?