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

Here's my current function,

 class emailer {
  function notifyHeart($post_ID)  {
    $interests = get_user_meta(get_current_user_id(), 'interests');
    $post = get_post($post_ID);

    foreach($interests as $interest) {
      if(has_tag($interest, $post)) {
        $to = get_the_author_meta( 'user_email', get_current_user_id() );  
        $email = $to;
        mail($email, "An article about Heart", 'A new post has been published!');
add_action('publish_post', array('emailer', 'notifyHeart'));

So far it only works with one user, the other user isn't receiving an email when there's a tag from post that match their interest.

Ideally, I would like to retrieve an email only when a user's $interest matches a tag from a post, then the appropriate email address will be selected and sent.

share|improve this question
Would changing, code $to = get_the_author_meta( 'user_email', get_current_user_id() ); to code$to = get_usermeta( $userid, 'user_email' ); make more sense? – Richard Dec 20 '12 at 21:35

Who should retrieve an email? The hook 'publish_post' is triggered when an author puiblish a post. So current user == post author.

I assume you would do that: If Buddy-A publish a post and Buddy-B is logged in at the same time, than Buddy-B should retrieve an email that Buddy-A published a post (which maybe interrest Buddy-B).

If I hit the nail and this is it what you want to do, forget it. WordPress have no centralized user management. You need a plugin ( see List of logged in users ) to get a list of all users currently logged in. Call your function for every user in the list and check if a interest match a post tag.

share|improve this answer
I see, thank you, that does make sense. Is it possible to retrieve a list of all users, even those who aren't logged in? That way it may be possible to cross reference each user and match those with their interests. – Richard Dec 20 '12 at 22:34
Yes, with get_users() (codex.wordpress.org/Function_Reference/get_users). I would exclude the current user ( $users = get_users( array( 'exclude' => get_current_user_id() ) ); ) to prevent sending the post author an email. – Ralf912 Dec 21 '12 at 22:28

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.