Fixing Comment Counts in Wordpress

As I’ve just migrated to using Wordpress for my blog, I thought I’d add some useful information in my first post.

Wordpress caches comment counts for each individual article in wp_posts.comment_count. If for some reason these cached values don’t match up to the real comment counts per article, you can fix this using the following SQL. This applies to Wordpress 2.3. I don’t know if it will work for anything else.

UPDATE wp_posts
  SET comment_count = (
   SELECT COUNT(*)
     FROM wp_comments
     WHERE wp_posts.ID=wp_comments.comment_post_id
     GROUP BY wp_comments.comment_post_id
   );

Leave a Reply