How to fix incorrect post comment counts In WordPress

If your WordPress comment counts got messed up, whether because of a plugin or you messed with your database manually and did something wrong, then there is always a solution for this 🙂

Comment Counts In WordPress

Here’s how comment counts work in WP:

  • Posts live in a table called wp_posts and each has an ID.
  • Comments reside in a table called wp_comments, each referring to an ID in wp_posts.
  • However, to make queries faster, the comment count is also cached in the wp_posts table, rather than getting calculated on every page load.

If this count ever gets out of sync with the actual number of comments for some reason, WordPress, while still displaying all comments properly, will simply show the wrong count.

How To Find Out Which Posts Are Out Of Sync

Fire up a MySQL shell or your favorite MySQL software and run this query.

It assumes your database is called wordpress and the prefix is wp_, so adjust those accordingly.

The result of this query is a list of posts whose comment_counts differ from the actual number of comments associated with each of them.

The left count is the cached number, while the right one is the right one.

How To Fix The Counts Automatically

Please make a backup of your database before performing any altering queries such as the one below.

The following query will recalculate and fix the comment counts for all posts that are out of sync (ones we just queried for above):

This will fix all your issues regarding comment count for sure.


Happy Coding 🙂

Leave a Reply

Notify of
Close Menu