Created
August 1, 2018 13:15
-
-
Save GaryJones/7b750277369e2c725fa871df0917b791 to your computer and use it in GitHub Desktop.
Fix WordPress comment counts (after incomplete import, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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