请尝试以下代码:
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\'");
}
或
you might want to try solution from this page (虽然这不是正确的方式,因为您将为每个帖子添加另一个查询)