Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Fix Category and Comment Count After WordPress Import

Importing your WordPress site can sometimes cause problems with the category and comment counts. It can be frustrating to see inaccurate numbers after all your hard work.

The good news is that there are simple ways to fix these issues, and they only require making a few adjustments.

In this article, we’ll show you how to fix your category and comment counts after WordPress import. Let’s get your site back in order!

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 comment count or none at all.

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

Fix comment count preview

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

In the following sections, let’s look at how to fix this issue and display an accurate comments count in WordPress.

Fixing Category and Comment Count in WordPress

Before we start, you’ll want to create a complete WordPress backup of your site. You should do this every time before making a major change to it.

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

For more details, please see our guide on how to back up your WordPress site with Duplicator.

After creating the backup, let’s move on to the next step. You’ll need to open a plain text editor like Notepad and copy-paste the following code:

<?php
include("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'");
        }
?>

Then, don’t forget 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‘s control panel or looking at your wp-config.php file using a file manager.

Once you have replaced the information, you can save the file as comments-fix.php on your desktop.

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

For more information, you might want to see our step-by-step guide on using FTP to upload files to WordPress.

After uploading the file to your website, let’s open your web browser and head over to this file:

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

Don’t forget to replace example.com with your site’s address.

Visiting this file in your browser will execute the script. It will loop through your posts, categories, tags, and comments to update the count.

Comment count fix

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

Bonus Tip: Configure Other Comment Settings

After importing your WordPress site, you might want to re-configure your comment settings.

First, you’ll want to decide whether you want to allow comments on new posts by default. Or you can just enable or disable comments for individual posts.

Moderation settings help you control the quality of comments. You can choose to approve comments manually or allow comments from trusted users to appear automatically.

Opening the Thrive Comments Moderation page on WordPress

Email notifications keep you updated about new comments. You can get notified whenever someone comments or when a comment needs approval. If you enable moderation, you can also notify users when their comments are approved.

Now, you might want to go to Settings » Discussion in your WordPress dashboard to configure your comment settings. However, we recommend using Thrive Comments for more advanced features.

Thrive Comment's homepage

Thrive Comments is the best WordPress comments plugin on the market. In addition to the basic configuration, it can help you engage your readers with features like upvotes, downvotes, likes, badges, and social media sharing.

For more details, you can see our Thrive review.

We hope this article helped you learn how to fix category and comment counts after WordPress import. Next, you may also want to check out our guides on how to show comments on the homepage of your WordPress theme and how to change the category order in WordPress.

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

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. Here's our editorial process.

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

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

    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)

    • WPBeginner Support

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

    Thanks for this tutorial! Once I switched to PHP 5.5 the script worked like a charm.

    • WPBeginner Support

      Glad it worked for you :)

      Admin

  3. Peter Edwards

    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?

    • Bonnie

      Ooo, such a simple solution, and it worked! Thanks!

  4. Kingsley Felix

    anyone for duplicate comments?

  5. Pare

    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.

  6. Phuc

    I get an error 500 if I want to access the comments-fix.php file. How can I fix this?

  7. Jay

    To echo Yuriy’s comments, it’s not working for me either with PHP 7. 500 error.

  8. Yuriy Smirnov

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

  9. Rianta D. Mulyana

    Thanks brother, it works!

  10. Cosmin

    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

    • Tyler

      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.

  11. Makinde

    Thanks WPBeginner, This was helpful..

    Thanks for always being there for us.

    Appreciate!

  12. Aeryn Lynne

    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.

  13. Naki Biga

    Work like a charm, thank you very much!

    Very simple and usefull code!

  14. Edwin Rio

    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 !

  15. union

    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 !

  16. Connor Rickett

    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.