How to Fix Category and Comment Count After WordPress Import

Are you noticing an incorrect comment count after the WordPress import? Importing a WordPress site using the designed-in importer can generally smash your WordPress comments count. While all the comments are safely imported and visual in the admin space, your posts can show an incorrect comment count on your web site. This same import error will also impact category count and custom taxonomy count. In this text, we will show you the way to fix class and comments count when importing WordPress.

Fix Category and Comment Count

As you noticed in the screenshot higher than, when the import our comment count and class count is showing zero rather than the particular number. Let’s have a look at how to mend it.

First you would like to form an entire WordPress backup of your web site. You should do that every time you’re visiting perform a serious modification on your website. We suggest using BackupBuddy, it is the foremost comprehensive WordPress backup plugin available.

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

Open an obvious text editor like Notepad and simply copy and paste the subsequent code:



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."<br />"; 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."<br />"; mysql_query("UPDATE ".$table_prefix."posts SET comment_count = '$count' WHERE ID = '$post_id'"); }


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

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

Once you’ve got replaced the information, save this file as comments-fix.php on your desktop.

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

After uploading the file to your web site, you need to open your net browser and go to the current file:

Replace 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 would like to delete comments-fix.php file from your server.

That’s all, we hope this text helped you update comments count once importing WordPress. You may additionally want to check out our guide on the most common WordPress errors and the way to fix them.