I recently migrated a large number of WordPress posts from one site to another and I noticed some odd behavior specifically in regards to the comments associated with imported posts. Some posts that were imported had comments associated with them, but for some reason the default Comment behavior which tells you the # of comments was reporting "No Comments" even though it clearly displayed comments from the post. After some research I found a nice code snippet that essentially forced the Database to recount the comments. So the instructions were to add a new code snippet, browse to a specific URL to initiate the recount, and then disable / remove the snippet – since having it accessible is clearly not something you want the public to be able to do. Here is the code snippet:
// Run once, then remove this code
if (isset($_GET['fix_comment_counts']) && current_user_can('manage_options')) WHERE post_type = 'post' AND post_status = 'publish'");
foreach ($posts as $post) WHERE comment_post_ID = %d AND comment_approved = '1'",
$post->ID
));
$wpdb->update($wpdb->posts, ['comment_count' => $count], ['ID' => $post->ID]);
}
echo '';
}
After you add the snippet and activate it. Then visit: https://yoursite.com/?fix_comment_counts
Important: Remove the code immediately after running it.




