This site is no longer being updated. Here is Andrew Parker's new blog.

Help with WordPress Comments Count

I was having trouble with my WordPress install, and I think I need a MySQL query (or perhaps a PHP script) to fix it…

Here’s the deal. I was getting flooded with spam so badly that stopped monitoring it for a month or so. A number of my posts got hosed with 200+ comments. The WordPress admin interface (in all its AJAX glory) was slowing to a crawl because it had to render so many posts. So I got fed up, and went straight into PHPMyAdmin to nuke my spam. It was a piece of cake because all of my spam was trackbacks, so I nuked everything marked “trackback” which is a simple enough query for me to write.

But, since I didn’t let my WordPress admin kill the spam, the admin couldn’t update the “quantity of comments” variable on my posts. See this archive page for an example of my problem (notice the absurd number of comments on each post that don’t actually exist).

So, I need a SQL query that takes a count of the comments that are tied to a given post and then inserts that count into the “quantity of comments” variable so that all my posts say the correct number of comments they have. Can anyone help me with this?

Perhaps my request is better suited for a newbie MySQL forum, but I thought I’d give it a stab here first.


2 Responses to “Help with WordPress Comments Count”  

  1. 1 lee

    I think you want this…
    update wp_posts wp set comment_count = (select count(*) from wp_comments wc where wc.comment_post_id = wp.id)

  2. 2 Andrew Parker

    Thanks Lee, you’re a rockstar!

Leave a Reply