Resetting comment count

Try this code:

WARNING: THIS IS JUST PSEUDOCODE!

$entries = $wpdb->get_results("SELECT * FROM wp_posts WHERE post_type IN ('post', 'page')");

foreach($entries as $entry)
{
    $post_id = $entry->ID;
    $comment_count = $wpdb->get_var("SELECT COUNT(*) AS comment_cnt FROM wp_comments WHERE comment_post_ID = '$post_id' AND comment_approved = '1'");
    $wpdb->query("UPDATE wp_posts SET comment_count="$comment_count" WHERE ID = '$post_id'");
}

Or you might want to try solution from this page (although it’s not the proper way as you will add another query for every post)

Leave a Comment