DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Perform Comment Recount For WordPress

05.14.2008
| 4068 views |
  • submit to reddit
        Run from the command line and uncomment line #2 if your mysql adapter is installed via RubyGems. Fast as hell.

#!/usr/bin/ruby
#require 'rubygems'
require 'mysql'
 
db = Mysql.real_connect("host", "username", "password", "database")
ids = db.query("SELECT `ID` FROM `wp_posts` WHERE 1")
 
ids.each_hash do |post|
  id = post['ID']
  num = 0
  comments = db.query("SELECT COUNT(1) FROM `wp_comments` WHERE `comment_post_ID`='#{id}' AND `comment_approved`='1';")
  comments.each {|x| num = x[0]}
  up = db.query("UPDATE `wp_posts` SET `comment_count`='#{num}' WHERE `ID`='#{id}';")
end