Easy Guide to Fix Category and Comment Count in WordPress

In this tutorial, we will learn how to fix category and comments count after importing WordPress.

As you noticed in the screenshot above, after the import our comment count and category count is showing 0 instead of the actual number. Let’s take a look at how to fix it.

Lets get started

First you need to create a complete WordPress backup of your site. You should do this every time you are going to perform a major change on your site. We recommend using BackupBuddy, it is the most comprehensive WordPress backup plugin on the market.

Once you have made the backup, let’s move on to the next step.

Open a plain text editor like Notepad and simply copy and paste the following code:

<?php
include("wp-config.php");
if (!mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)) {  die('Could not connect: ' . mysql_error());  }
if (!mysql_select_db(DB_NAME)) {  die('Could not connect: ' . mysql_error());  }
 
$result = mysql_query("SELECT term_taxonomy_id FROM ".$table_prefix."term_taxonomy");
while ($row = mysql_fetch_array($result)) {
  $term_taxonomy_id = $row['term_taxonomy_id'];
  echo "term_taxonomy_id: ".$term_taxonomy_id." count = ";
  $countresult = mysql_query("SELECT count(*) FROM ".$table_prefix."term_relationships WHERE term_taxonomy_id = '$term_taxonomy_id'");
  $countarray = mysql_fetch_array($countresult);
  $count = $countarray[0];
  echo $count."
";
 mysql_query("UPDATE ".$table_prefix."term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term_taxonomy_id'");
        }
 
$result = mysql_query("SELECT ID FROM ".$table_prefix."posts");
while ($row = mysql_fetch_array($result)) {
  $post_id = $row['ID'];
  echo "post_id: ".$post_id." count = ";
  $countresult = mysql_query("SELECT count(*) FROM ".$table_prefix."comments WHERE comment_post_ID = '$post_id' AND comment_approved = 1");
  $countarray = mysql_fetch_array($countresult);
  $count = $countarray[0];
  echo $count."
";
  mysql_query("UPDATE ".$table_prefix."posts SET comment_count = '$count' WHERE ID = '$post_id'");
        }
?>

You need to replace DB_HOST, DB_USER, DB_PASSWORD with your WordPress database host (usually localhost), database username, and password.

You can find all this information by logging into your WordPress hosting cPanel or by looking at your wp-config.php file using a file manager.

Once you have replaced the information, save this file as comments-fix.phpon your desktop.

Now you will need to upload this file to your site’s root directory. You can do that by using an FTP client or by using the file manager in your web hosting control panel.

After uploading the file to your website, you need to open your web browser and go to this file:

http://example.com/comments-fix.php

Replace example.com with your site’s address.

Visiting this file in your browser will run the script which simply loops through your posts, category, tags, comments, etc and update the count.

Fixing taxonomy terms and comment count numbers

Important: Once you’re done fixing your WordPress comment count, you need to delete comments-fix.php file from your server.

That’s all, we hope this tutorial helped you update comments count after importing WordPress.

Leave a Reply