Skip to content

Instantly share code, notes, and snippets.

@jeffreyvr
Created April 1, 2015 10:51
Show Gist options
  • Save jeffreyvr/f81010172de15672e7d5 to your computer and use it in GitHub Desktop.
Save jeffreyvr/f81010172de15672e7d5 to your computer and use it in GitHub Desktop.
WordPress recount comments
<?php
/**
* Put this file in the root of your WordPress installation.
* When done remove it!
*/
include 'wp-blog-header.php';
global $wpdb;
$wp_posts = $wpdb->get_results("SELECT ID FROM wp_posts WHERE post_status = 'publish'");
foreach( $wp_posts as $post ) {
$post_id = $post->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'");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment