Beginner's Guide for WordPress / Start your WordPress Blog in minutes

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 built-in importer can sometimes mess up your WordPress comments count. While all the 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. In this article, we will show you how to fix category and comments count after importing WordPress.

Fix Category and Comment Count

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.

Video Tutorial

Subscribe to WPBeginner

If you don’t like the video or need more instructions, then continue reading.

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."<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 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.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.

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 article helped you update comments count after importing WordPress. You may also want to check out our guide on the most common WordPress errors and how to fix them.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Google+.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

21 CommentsLeave a Reply

  1. Mine is not working, all the count from comments-fix.php is appear, but when i go to single page product it still says Reviews(0)

    • Your server may be running on a newer PHP version, this code was for php 5.5 and we will certainly take a look into what we can do to update the code.

      Admin

  2. I lost most of the tag counts in a recent import – fixed this by bulk editing all the posts which were tagged and clicking update (without any changes). This triggers the tag count to be updated by wordpress core. Not sure if a similar trick would work for comments?

  3. Just using cPanel go to PhpMyAdmin

    Check What is category id (term_id) in table “xxx_terms” and then update count value in column count into the table “xxx_term_taxonomy” where the term_id is equal term_id of xxx_terms table which that category exists on.

    Forexample “uncategory” of product is term_id = 10 and count value is wrong, I update count value to be correct number in table “xxx_term_taxonomy” where term_id = 10. by manual on cPanel – PhpMyAdmin tool.

  4. Doesn’t work on php7, because mysql_connect function has been removed since 7.0.0 verstion.

  5. Quick question: when replacing the DB_HOST, DB_USER, DB_PASSWORD and DB_NAME do we need to use “” or ” to enclose those values in? Or do we simply paste the values?
    I tried and I am getting 500 Internal Server Error while accessing the uploaded file

    • Yes, you do need to include single quotes around those values. Author should update the snippet to reflect that. Also, author forgot to mention that DB_NAME must also be changed.

      Easy to spot, but this tutorial will fail for anyone following the instructions explicitly.

  6. After a disastrous comment import that involved Intense Debate (only thing we could do is import bits of the xml file directly into mysql after obtaining post IDs for nearly a thousand posts,) I’m definitely in need of a quick program like this to correct the count, so thank you!

    If anyone is looking to fix the comment count for one or two posts only though: instead of accessing FTP and mysql, they just need click Edit on one of the comments of the post that needs correction, and then click Update, and the post then updates the comment count for all comments involved in that post.

  7. hi …

    i have migrated one sito into another (i was using disqus for comments)

    now in my new site, i can see the comments on the comments area but once i activate the disqus plugin the comments are not appearing on the front end article,

    is this solution for my problem as well or my problems its most on disqus side ?

    Thanks !

  8. hi …
    i have migrated one sito into another (i was using disqus for comments)

    now in my new site, i can see the comments on the comments area but once i activate the disqus plugin the comments are not appearing on the front end article,

    is this solution for my problem as well or my problems its most on disqus side ?

    Thanks !

  9. That seems fairly straightforward. Thanks for putting that code together, that’s a huge help!

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.