September 24, 2006
Recalculate WordPress comment count 1
find more posts in: ruby, wordpressSo I’ve been doing some work on the long-neglected Fontleech and today found myself removing over .25 million spam comments. That sounds herculean, but it was actually pretty easy.
The WordPress frontend pulls the comment count for each post from the ‘comment_count’ field in the wp_posts table. obviously that doesn’t get updated when you manually delete spam directly from the database, so here’s a simple ruby script that will update that for you (just define your db variables):
#!/usr/bin/ruby require 'mysql' db = Mysql.real_connect(host, user, pass, dbname) 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
1 Response
Leave a Reply
My name is Joey Nelson. I'm a web developer living in Raleigh, NC. I'm married to an awesome lady and we have an awesome one year old son. If you'd like to find out more about my fascinating life, check this stuff out:
Track me
Visit my sites
Other stuff
Latest posts
- Google ASCII Art Easter Egg
- Is it so crazy that someone would play VATFUL?
- Now that’s a quesadilla
- Automatically playing audio with HTML5 and Javascript (even on the iPad)
- Prevent console.log() errors
- Tumblin’
- omgtru updates and iPhone web app lessons learned
- Really?
- Easy form hints: HTML5 input placeholders
- Anthem Type
Categories
- apple
- art
- audio
- baseball
- bikes
- books
- browsers
- comedy
- complaints
- crime
- death
- dumb things
- energy
- environment
- family
- fonts
- food
- friends
- gadgets
- games
- henry
- html
- internet
- ipad
- iphone
- itunes
- javascript
- jquery
- life
- los angeles
- marriage
- movies
- msgblorb
- music
- mysql
- new york
- omgtru
- os x
- pasadena
- photos
- php
- plugins
- podcasting
- politics
- portland
- pugs
- rss
- ruby
- splideo
- subversion
- ubuntu
- Uncategorized
- video
- watir
- wordpress
- work
Hey Joey, I have this problem too but I dont idea how to make this working.
Would you mind telling me how I execute the script?