WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I want here is simple, but I'm not really sure how to approach it. I have a tournament website based on WordPress, where users sign up for a tournament by commenting on a post.

The problem is, that then I need to send all those guys an email, which means I have to look up 50+ emails for users every single time. I'm thinking of doing some kind of JOIN on the comments and users tables, but I don't really know how to put this in WordPress.

I read somewhere, that if I want to add stuff for users, it should be in a theme, and if I want to add it for administrators, it should be as a plugin, but this looks like it could be a simple script put somewhere.

How would you solve this?

share|improve this question
up vote 3 down vote accepted
 global $wpdb, $post;
 $query = sprintf("SELECT comment_author_email
                     FROM {$wpdb->comments}
                     JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID
                     WHERE comment_post_ID = %d
                       AND comment_approved = '1'",
 $emails = $wpdb->get_col($query);
 $emails = array_unique($emails);

 // your e-mails

You can also use the built-in WP_Comment_Query (or get_comments), but it's going to be a little slower...

share|improve this answer
What does %d refer to, does it get the value from global post ? – Elliott Sep 18 '11 at 15:22
%d is the next sprintf argument, casted to integer, In this case $post->ID. You can replace it with $post->ID, it's the same thing... – onetrickpony Sep 18 '11 at 17:52
Thanks i'm always seeing them floating about ! – Elliott Sep 18 '11 at 19:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.