MrQwest

Getting My Hands Dirty

A few months ago, I was tweaking a couple of items on UKG★Mix (one of my side-projects). I wanted to add a comment to the footer of the page which would output a count of how many mixes on the website.

That is easier said than done. If I had just posted mixes, it’d be fine. Output the post count, and all is well… but I do have a ‘dev-blog’ running off the same WP install and because these posts aren’t mixes, using a ‘post-count’ to total all posts wouldn’t work!

So what I needed was a plugin that would output a total post-count minus a specific category.

I came across the postCountMinusCat plugin by Justin Blanton which done exactly what I needed… however, it didn’t work for me!

Gutted.

Anyway, I contacted Justin via twitter to see if he could offer any guidance. Unfortunately he hadn’t come across the same error before so couldn’t help me.

He did however give me a lead into what the problem could be! The plugin was outputting a number, but it was the total posts. It just wasn’t removing the post-count from the dev-blog category.

Armed with a cup of coffee, I dived into the plugin to see what was up. I followed the code and cross-referenced it with my MySQL database and realised that the plugin was referencing the category table in the database ($wpdb->category) however it wasn’t there in my database. Odd.

Anyway, I found the right table, and changed a couple of the queries in the plugin and actually got the plugin working!

So for the purpose of future reference, I’ve posted the updated source code here (& also let Justin know) so that if anyone else is having the same problem, they just need to swap Justin’s code with mine… more specifically, these 2 lines need to be changed.

// get number of posts in specified category
$numPostsCat = $wpdb->get_var("SELECT count FROM $wpdb->term_taxonomy where term_id = $catID");

Admittedly, it’s not a complicated plugin but I do not have any experience with this sort of thing so am extremely pleased that I managed to fix it!

Source Code

<?php
/*
Plugin Name: Post Count minus Category
Version: 1.0
Plugin URI: http://justinblanton.com/projects/postcountminuscat/
Description: Displays the total number of posts not belonging to the specified category.
Author: Justin Blanton
Author URI: http://justinblanton.com
*/   
function postCountMinusCat($catID) {
global $wpdb;
// get total number of posts    
$numPosts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); 
// get number of posts in specified category
$numPostsCat = $wpdb->get_var("SELECT count FROM $wpdb->term_taxonomy where term_id = $catID"); 
// calculate number of posts not in specified category
echo $numPosts - $numPostsCat;
} ?>@

Previously:

Next:

Search

Hire Me