Step-by-Step Guides to Fix Category and Comment Count After Import

Step-by-Step Guides to Fix Category and Comment Count After Import

Hi Guys,

Recently we’ve received some questions from our customers about category and comment count error after WordPress Import.

As you know, the WordPress Importer will import data including pages, posts, comments, custom fields and post meta, categories, tags and terms from custom taxonomies and author content from a WordPress export file.

Unfortunately, importing a WordPress site using the built-in importer can sometimes produce an incorrect comment, category and custom taxonomy count afterwards.

In this post we will show you how to fix the WordPress Importer comment and category count errors.

Back up Your Site

First, you need create a complete backup of your WordPress site. You should do this every time you are going to make a major change on your site. A simple way to accomplish this is with BackupBuddy, the most popular WordPress backup plugin on the market.

Step-By-Steps Guides

To fix this annoying WordPress Importer problem, you simply copy and then upload the following code to the root directory of your WordPress site.

So, using a plain text editor like Notepad, create a php file called, wp-import-fix.php, containing the code shown below.

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

 
 <?php
 // Filename: wp-import-fix.php
 // Place this file in your root directory and navigate to it.
 // Script will correct counts for comments and categories.
  
 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."<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'");
 }
 ?>

 

Upload this file to the root directory of your WordPress site using an FTP client such as FileZilla 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://yourwebsitename.com/wp-import-fix.php

The script simply loops through your posts, category, tags, comments, etc and updates the counts.

Important: Once you’re done fixing your WordPress comment count, delete the, wp-import-fix.php, file from your server.

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

Related News

Leave Your Comment