問題描述
最近我不得不轉到另一個域,不得不在 enw 伺服器上重新建立我所有的 MySQL 表。我注意到,對於一些帖子,評論數量翻了一番。示例 – http://inversekarma.in/scraps/you-dont-mess-with-the-rajinikanth/。
在上面的帖子中,只有 4 條評論,但評論鞋 8 評論。我不知道它不是一個 theme-related 問題,因為這隻發生在幾個帖子。
有人可以告訴我如何編寫 MySQL 查詢來重新計算所有帖子的評論?還是外掛?
最佳解決方案
嘗試這段程式碼:
警告:這是一個 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(雖然這不是正確的方式,因為您將為每個帖子新增另一個查詢)
次佳解決方案
似乎是一個常見的問題。嘗試這個外掛:
http://wordpress.org/extend/plugins/web-ninja-comment-count-fixer/
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。