Skip to content

Instantly share code, notes, and snippets.

@GaryJones
Created August 1, 2018 13:15
Show Gist options
  • Save GaryJones/7b750277369e2c725fa871df0917b791 to your computer and use it in GitHub Desktop.
Save GaryJones/7b750277369e2c725fa871df0917b791 to your computer and use it in GitHub Desktop.
Fix WordPress comment counts (after incomplete import, etc.
<?php
// Can drop code into top of {yourtheme}/functions.php and then hit the site.
// Fix comment counts.
$post_types = ['post']; // Set to the post types to fix.
$post_status = ['publish', 'pending', 'draft']; // Set to the post status to fix.
global $wpdb;
$posts = $wpdb->get_results(
'SELECT ID FROM ' . esc_sql( $wpdb->posts ) . ' WHERE post_type IN ("' . implode( '","', array_map( 'esc_sql', $post_types ) ) . '") AND post_status IN("' . implode( '","', array_map( 'esc_sql', $post_status ) ) . '")'
);
if ( ! empty( $posts ) && is_array( $posts ) ) {
foreach ( $posts as $post ) {
wp_update_comment_count_now( $post->ID );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment