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?

1 Answer 1

 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...

  • What does %d refer to, does it get the value from global post ?
    – Elliott
    Sep 18, 2011 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... Sep 18, 2011 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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