• Hi there,

    Trying to figure out a mystery and just can’t – maybe someone can help:
    I would like an email to be sent to the author of a custom post after I, as the admin, publish the post (change status from pending to published). I used the code on this blog post, adding it to my functions.php file in my theme:
    Notify Authors

    The code is as followed:

    //Notify Authors when Article Published
    function wpr_authorNotification($post_id) {
       $post = get_post($post_id);
       $author = get_userdata($post->post_author);
    
       $message = "
          Hi ".$author->display_name.",
          Your post, ".$post->post_title." has just been published. Well done!
       ";
       wp_mail($author->user_email, "Your article has been published. Please check the article and let us know if anything needs to be changed. Please do not forget to Tweet, Like, and Google +1 the article. ", $message);
    }
    add_action('publish_listing', 'wpr_authorNotification');

    The code does work, HOWEVER, when it sends two emails instead of 1 to the user – no matter what. Interestingly, if the add_action line is:
    add_action(‘publish_post . . . instead of publish_listing (listing is the name of my custom post type), then it sends only 1 email.

    Why would a custom post type cause the system to generate 2 emails instead of 1?

    So confused -thanks in advanced!

    kevin

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t have an explanation, but I’m not surprised. Weird stuff happens in relation to status transition hooks. You might look into the Transients API to set a temporary flag so an email is only sent once per post ID, no matter how many times an action fires for one post.

    Thread Starter ruess

    (@ruess)

    Thanks bcworkz – that sounds plausible – yet complicated 🙂 If anyone has any guidance on how to structure this that would be very helpful – not even sure where to start.

    Thanks!

    kevin

    Thread Starter ruess

    (@ruess)

    Very roughly, with this definition in mind:

    set_transient( $transient, $value, $expiration );

    would it be something to the effect of:

    set_transient( [my function above], $my-email-message, $expiration );

    ? Hmm. .that doesn’t seem to make much sense does it? (scratching head)

    Moderator bcworkz

    (@bcworkz)

    I think you see a transient as a variant of schedule task. It’s more of a metadata that’s self deleting. $transient is any convenient tag, much like with filters. $value is like metadata. In your case, all that really matters is the value exists, but perhaps it should be an identifier, like the post ID, to ensure the transient is associated with the same process and not a parallel one. Of course, $expiration is the time until the data self deletes.

    The problem is the publish_listing action apparently fires twice for one post for no apparent reason. Your action function would, on entry, check for the transient. If it exists and matches the post ID, return without doing anything. If it does not exist, send the email and set the transient in case this function is called again in the near future.

    Makes sense now?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Sending 2 duplicate emails when custom post published’ is closed to new replies.