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!

View Comments to Selecting a Random Blog Comment From Your WordPress Database
  1. Brad V.
    January 1, 2008 | 11:14 pm

    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!

  2. Maxime Guernion
    January 1, 2008 | 11:27 pm

    You could add :

    WHERE `comment_post_ID` = 356 AND `comment_type` != ‘pingback’

    if you only want comments without pingback.

  3. jhay
    January 2, 2008 | 1:53 am

    Sounds fun and easy enough. Though I need to have lots of comments one a post for this to be truly effective.

  4. pelf
    January 2, 2008 | 4:53 am

    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! :D And may the new year brings you more hope, joy, success, happiness, love and all things sweet and beautiful! :D

  5. Chris Garrett
    January 4, 2008 | 1:48 pm

    @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 :)

blog comments powered by Disqus