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!

If you found this article useful, bookmark it at Del.icio.us for future reference
Subscribe now with RSS, daily emails or weekly emails to receive more tips, new media news and a FREE ebook!
Business Blogging Ebook

5 Comments so far - add yours now

  1. 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. You could add :

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

    if you only want comments without pingback.

  3. jhay 

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

  4. pelf 

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

Due to the quantity of spam comments this blog attracts, comments close on new posts soon after publishing.

Authority Blogger

Free Gifts

Receive more free blogging, writing and marketing tips to your email inbox or feed reader, plus a free eBook download.

Feed Count Subscribe now with RSS or
get articles by Email

Subscribing is free, and your email will never be shared

About Chris Garrett

Chris Garrett is a blogging and internet marketing consultant. This blog is here to help you make the most out of the web.

Follow me on twitter Read more about Chris and this blog.

View Chris Garrett's profile on LinkedIn FaceBook

Search this site

  • Popular Articles

  • Recommends

  • Categories

      Archives

      Hosting By