Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 4 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'",
                  $post->ID);
 $emails = $wpdb->get_col($query);
 $emails = array_unique($emails);

 // your e-mails
 print_r($emails);

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

 
discard

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.