Selecting a Random Blog Comment From Your WordPress Database
Bloggers like to give and win prizes. It offers some excitement for readers, content and links for the blogger, and if the prize is offered by a third party it gives them some exposure too. Darren does this a lot and to good effect, I do it very occasionally when I have something good to provide as a prize.
One question I get asked is how exactly do I select a random comment? You probably can make or find a plugin but I just use some simple SQL to read the WordPress database.
Most hosting providers offer a PHPMyAdmin tool so you can run the SQL through a simple web interface. All you need then is your post ID which can be found in your WordPress post editing screen. Hover over the Edit link for the post you want to select comments from and it will say in the status bar a URL ending in ‘post=’. The number that follows is what you need to note down.
In my example the post was numbered 356 so my query ends up as:
SELECT *
FROM `wp_comments`
WHERE `comment_post_ID` = 356
ORDER BY RAND()
LIMIT 3 ;
Rand does the job of making the results random. Limit says how many results you want, if you only have one winner then make it 1.
As I said, no doubt someone has made or will make a plugin, but being a geek I find this quite efficient enough!
Posted on January 01st, 2008 by Chris Garrett in Development











It’s funny you should mention this as I just had my very first comment contest on my own blog. Since I don’t use Wordpress for my blogging platform (I use Typepad instead).
To pick a random comment, I went to the website random.com and chose the option to pick a random integer. I plugged numbers 1 to whatever number of comments there were. It picked a random number, and that was the winning comment!
I will say that I underestimated contests like these. It brought a huge spike in traffic to my blog even though it wasn’t announce very far ahead of time.
Great post!
You could add :
WHERE `comment_post_ID` = 356 AND `comment_type` != ‘pingback’
if you only want comments without pingback.
Sounds fun and easy enough. Though I need to have lots of comments one a post for this to be truly effective.
I have never hosted a contest because I do not have anything to give away as prizes
But if I did host one, I think I would just write names into pieces of paper and randomly pick one (because MySQL is a total stranger to me!), LOL.
Anyway, Happy New Year, Chris!
And may the new year brings you more hope, joy, success, happiness, love and all things sweet and beautiful! 
@Brad - I think done sparingly contests can work well, the trick is to have something good to give away and to not fill your blog with competition noise
@Maxime - Nice addition
@jhay - Yeah you need to have a good selection which usually means promoting unless you have a lot of comments on most posts
@pelf - Happy new year
You are so old school
, but it’s always best to do what works for you 