Category Posts Count Plugin

Category Posts Count is a plugin for WordPress 2.0 (not tested for earlier versions) that lets you count the number of posts in a category, using a newly created function. It uses moderately complex SQL constructs. The only function generates a number, if you give the category name as a parameter.In my search for a nice look for the sidebar, something like:

thus with category post counts, I got really disappointed by the community, not providing this feature at all. So I had to rely on my own intuitive thinking and made my first (quick and dirty) plugin.
Here is the php code:

			INNER JOIN $wpdb->post2cat
			INNER JOIN $wpdb->posts
			WHERE cat_ID = category_id
			AND post_id = $wpdb->posts.ID
			AND post_status = 'publish'
			AND cat_name = '$category'
			GROUP BY cat_ID";
	$dbresult = $wpdb->get_results($query);
	return $dbresult[0]->count;

and the corresponding Readme file:

Category Posts Count
by Sebastiaan Mindreau
0:12 9/11/2006

This plugin requires that you change core files.
If you are unsure, DON'T proceed.

1. Copy categoryPostsCount.php to wp-content/plugins.
2. Activate the plugin in the Administration panel -> plugins.
3. Edit the file wp-includes/template-functions-category.php.
4. Find the last occurence of "foreach ( $categories as $category ) {".
   This routine prints out the cateogries, when called by the template
   using the wp_list_cats() function.
5. Change whatever you want in the following lines, but make sure you 
   add valuable changes, for example I changed:
$link .= apply_filters('list_cats', $category->cat_name, $category).'';
$link .= apply_filters('list_cats', $category->cat_name, $category).' ('.sgcam_category_count($category->cat_name).')';
and now it prints the number of posts for every category between parentheses.

Use this plugin at your own risk.  It will (normally) not harm your database but
may (by still unreported bugs) break your theme.  I give you no warranty whatsoever.

This plugin is released under the GPL public license, as is WordPress.

I’m not publishing it yet to the community, due to imperfections in the code (which may cause your webserver to get heavier loads and stuff). It’s my first plugin, and it was written in about 10 minutes, so don’t expect too much. Contact me if you want it to work anyway, but can’t make it work. Don’t forget: you must change the core code yourself!

Note that it is not a bug that the sums of category posts count don’t match, as some posts may belong to more than one category, as is the case on this site for example.

I hope I was of any help by providing this plugin here.


The following bugs were fixed:

  • 09/11/2006 0.45: Don’t count the unpublished items.


The following bugs are still unresolved:

  • 01/01/2007 15.00: Doesn’t work with category names with special characters, watch out for SQL injections! Only possible for administrators of course. So only administrators can cause the db queries to break because of extra quotes like .

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s