Praveen's Blog

An Eternal Quest for Incremental Improvement

Fixing comments count in Wordpress

For some reason, after upgrading to Wordpress 2 I face some problems with the comments count. Googling showed that there was some problem with the SpamKarma plugin. But I don't have SpamKarma installed. My comment count won't decrement if I delete some spam trackbacks through the admin interface. The wrong comment count will continue to be there forever. Here is a small script to fix this issue.

if (!file_exists('../wp-config.php'))
    die("There doesn't seem to be a wp-config.php file. Double check that you updated \\
 wp-config-sample.php with the proper database connection information and renamed it \\
to wp-config.php.");

function fixit(){
        global $wpdb;

        $query = "UPDATE wp_posts SET comment_count = (SELECT COUNT(comment_post_id) FROM \\
wp_comments WHERE = wp_comments.comment_post_id)";

        $comments = $wpdb->query($query);


Save this script in a file in the wp-admin directory of your Wordpress install and invoke it through the browser or PHP command line. This will fix the comment count. Once your purpose is solved, delete the file.