Sign up right now for email updates and get two free ebooks:

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!

Sign up right now for email updates and get these
two free ebooks

"Creating Killer
Flagship Content"

"Authority Alliances"

Just enter your primary email address in the form below and hit the button!

Feed Count

chrisg.com runs on the Genesis Framework

Genesis Framework

Genesis empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go. It's that simple - start using Genesis now!

Take advantage of the 6 default layout options, comprehensive SEO settings, rock-solid security, flexible theme options, cool custom widgets, custom design hooks, and a huge selection of child themes ("skins") that make your site look the way you want it to. With automatic theme updates and world-class support included, Genesis is the smart choice for your WordPress website or blog.

Comments

  1. Brad V. says:

    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. Brad V. says:

    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!

  3. You could add :

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

    if you only want comments without pingback.

  4. You could add :

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

    if you only want comments without pingback.

  5. jhay says:

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

  6. jhay says:

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

  7. pelf says:

    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

  8. pelf says:

    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

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

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