WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Fix Category and Comment Count After WordPress Import

How to Fix Category and Comment Count After WordPress Import

Last updated on July 30th, 2015 by Editorial Staff
128 Shares
Share
Tweet
Share
Pin
Special WordPress Hosting offer for WPBeginner Readers
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+.

128 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

  • Google Analytics in WordPress

    How to Install Google Analytics in WordPress for Beginners

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

20 Comments

Leave a Reply
  1. Fitri says:
    Apr 8, 2019 at 10:56 pm

    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)

    Reply
    • WPBeginner Support says:
      Apr 9, 2019 at 11:59 am

      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.

      Reply
  2. Carlos says:
    Jan 15, 2019 at 6:30 pm

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

    Reply
    • WPBeginner Support says:
      Jan 16, 2019 at 10:48 am

      Glad it worked for you :)

      Reply
  3. Peter Edwards says:
    Oct 11, 2018 at 7:58 am

    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?

    Reply
  4. Kingsley Felix says:
    Sep 23, 2018 at 2:18 pm

    anyone for duplicate comments?

    Reply
  5. Pare says:
    May 27, 2018 at 6:09 pm

    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.

    Reply
  6. Phuc says:
    Feb 27, 2018 at 1:28 am

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

    Reply
    • WPBeginner Support says:
      Feb 27, 2018 at 6:16 pm

      Hello,

      Please see our guide on how to fix 500 internal server error.

      Reply
  7. Jay says:
    Sep 28, 2017 at 2:02 pm

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

    Reply
  8. Yuriy Smirnov says:
    Jul 14, 2017 at 9:21 am

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

    Reply
  9. Rianta D. Mulyana says:
    May 1, 2017 at 3:55 am

    Thanks brother, it works!

    Reply
  10. Cosmin says:
    Nov 22, 2016 at 10:10 am

    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

    Reply
    • Tyler says:
      Jan 18, 2017 at 2:53 pm

      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.

      Reply
  11. Makinde says:
    Nov 14, 2016 at 12:03 am

    Thanks WPBeginner, This was helpful..

    Thanks for always being there for us.

    Appreciate!

    Reply
  12. Aeryn Lynne says:
    May 14, 2016 at 9:13 am

    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.

    Reply
  13. Naki Biga says:
    Nov 9, 2015 at 11:57 am

    Work like a charm, thank you very much!

    Very simple and usefull code!

    Reply
  14. Edwin Rio says:
    Jul 26, 2015 at 7:37 pm

    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 !

    Reply
  15. union says:
    Jul 26, 2015 at 7:01 pm

    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 !

    Reply
  16. Connor Rickett says:
    Jul 24, 2015 at 7:57 am

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

    Reply

Leave a Reply Cancel 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.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
WPForms Logo
WPForms
Drag & Drop WordPress Form Builder Plugin. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 25 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2019 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2019)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2019)
    • SiteGround Reviews from 1032 Users & Our Experts (2019)
    • Bluehost Review from Real Users + Performance Stats (2019)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • 7 Best CRM Software for Small Businesses (Compared)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2019 – Step by Step Guide
Deals & Coupons (view all)
Weglot Coupon
Get 15% OFF on Weglot multilingual plugin for WordPress.
Webnus
Webnus Coupon
Get 20% OFF on Webnus themes and plugins for WordPress.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress

Copyright © 2009 - 2019 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by HostGator | WordPress CDN by MaxCDN | WordPress Security by Sucuri.