How to Fix Category and Comment Count After WordPress Import

Are you noticing an incorrect comment count after importing your WordPress site?

Importing a WordPress site using the built-in importer can sometimes mess up your WordPress comments count.

In this article, we will show you how to fix category and comments count after importing WordPress.

How to Fix category and comment count after WordPress import

What Causes Category and Content Count Error in WordPress?

When you import your WordPress site using the built-in importer, it can sometimes show an inaccurate comments count or none at all.

While all the WordPress comments are safely imported and visible in the admin area, your posts will show an incorrect comment count on your website. This same import error can also impact category count and custom taxonomy count.

Fix comment count preview

As you noticed in the screenshot above, after the import our comment count and category count are showing 0 instead of the actual number.

That said, let’s take a look at how to fix this issue and see accurate comments count in WordPress.

Fixing Category and Comment Count in WordPress

First, you need to create a complete WordPress backup of your site. You should do this every time before performing a major change on your site.

We recommend using Duplicator because it is the most comprehensive WordPress backup plugin on the market.

For more details, please see this guide on how to backup your WordPress site with Duplicator.

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

Next, open a plain text editor like Notepad and simply copy and paste the following code:

12345678910111213141516171819202122232425262728<?phpinclude("wp-config.php");$myConnection= mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD);if(!mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD)) {  die('Could not connect: '. mysqli_error());  }if(!mysqli_select_db($myConnection, DB_NAME)) {  die('Could not connect: '. mysqli_error());  } $result= mysqli_query($myConnection, "SELECT term_taxonomy_id FROM ".$table_prefix."term_taxonomy");while($row= mysqli_fetch_array($result)) {  $term_taxonomy_id= $row['term_taxonomy_id'];  echo"term_taxonomy_id: ".$term_taxonomy_id." count = ";  $countresult= mysqli_query($myConnection, "SELECT count(*) FROM ".$table_prefix."term_relationships WHERE term_taxonomy_id = '$term_taxonomy_id'");  $countarray= mysqli_fetch_array($countresult);  $count= $countarray[0];  echo$count."<br />"; mysqli_query($myConnection, "UPDATE ".$table_prefix."term_taxonomy SET count = '$count' WHERE term_taxonomy_id = '$term_taxonomy_id'");        } $result= mysqli_query($myConnection, "SELECT ID FROM ".$table_prefix."posts");while($row= mysqli_fetch_array($result)) {  $post_id= $row['ID'];  echo"post_id: ".$post_id." count = ";  $countresult= mysqli_query($myConnection, "SELECT count(*) FROM ".$table_prefix."comments WHERE comment_post_ID = '$post_id' AND comment_approved = 1");  $countarray= mysqli_fetch_array($countresult);  $count= $countarray[0];  echo$count."<br />";  mysqli_query($myConnection, "UPDATE ".$table_prefix."posts SET comment_count = '$count' WHERE ID = '$post_id'");        }?>

Hosted with ❤️ by WPCode

1-click Use in WordPress

You need to replace DB_HOST, DB_USER, DB_PASSWORD with your WordPress database host (usually the 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.php on 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.

You follow our step-by-step guide on how to use FTP to upload files to WordPress for more information.

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

https://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, and updates the count.

Comment count fix

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

We hope this article helped you learn how to fix category and comments counts after WordPress import. You may also want to check out our guide on how to solve the most common WordPress errors and our ultimate WordPress SEO guide to improve your rankings.

Are you noticing an incorrect comment count after importing your WordPress site? Importing a WordPress site using the built-in importer can sometimes mess up your WordPress comments count. In this article, we will show you how to fix category and comments count after importing WordPress. What Causes Category and Content…

Are you noticing an incorrect comment count after importing your WordPress site? Importing a WordPress site using the built-in importer can sometimes mess up your WordPress comments count. In this article, we will show you how to fix category and comments count after importing WordPress. What Causes Category and Content…

Leave a Reply

Your email address will not be published. Required fields are marked *