-1

I have the following function that emails authors of a custom post type when the post is published which is great, but I would like it so that it does not email them on any future edits after that to avoid sending them loads of emails by accident.

This is the function I have at the moment:

//email authors
function authorNotification($post_id) {
   $post = get_post($post_id);
   $author = get_userdata($post->post_author);

   $message = "Hi ".$author->display_name.".
A document for you, ".$post->post_title." has just been published or edited and requires your attention. 
You will be taken to the document after you log in from this link: ".get_permalink( $post_id ).".
   ";
   wp_mail($author->user_email, "An online document has been made or edited for you.", $message);
}
add_action('publish_portaldocuments', 'authorNotification');

I'm wondering if it has something to do with the The {$old_status}to{$new_status} and {$new_status}_{$post->post_type} hooks as on this page: Post publish only hook?

Could someone help please as I'm not sure how I would implement it WITH A CUSTOM POST TYPE. Thanks :-)

Update: This worked for me:

// SEND EMAIL TO AUTHOR OF A CUSTOM POST TYPE ONCE POST IS PUBLISHED

function authorNotification($post_id) {
    if( ( $_POST['post_status'] == 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) {
 $post = get_post($post_id);
   $author = get_userdata($post->post_author);

   $message = "Hi ".$author->display_name.".
A document for you, ".$post->post_title." has just been published or edited and requires your attention. 
You will be taken to the document after you log in from this link: ".get_permalink( $post_id ).".
   ";
   wp_mail($author->user_email, "An online document has been made or edited for you.", $message);
    }
}

add_action( 'publish_portaldocuments', 'authorNotification' );
1
  • 1
    I have updated the linked answer with a post type restriction.
    – fuxia
    Commented Apr 21, 2014 at 16:39

1 Answer 1

0

Yes, it sounds like you do want a post transition hook, probably draft_to_publish as per the following from the Codex:

function your_callback( $post ) {
    // Code here
}
add_action( 'draft_to_publish', 'your_callback' );

Use authorNotification-- your function name-- instead of your_callback. It should be fairly simple in your case.

However, the precise details of your project are not clear. You may require something more complex. I can't tell.

6
  • 1
    An alternative to the draft_to_publish hook would be if you wanted to make this a one hit without relying on the status changes. In some editorial workflows the status of the content may change over time, as such the notifications might be falsely triggered. Adding an additional lookup to a custom meta field within the draft_to_publish hook (as mentioned by s_ha_dum) it would allow the ability to resend later if you really need too but not limit to only status changes. Commented Apr 21, 2014 at 15:01
  • I appreciate your help.Basically instead of getting emails every time the post is updated, I just want to send the message if it is first published. I can't make this 'draft_to_publish' work with my custom post type 'portaldocuments'.
    – Chris
    Commented Apr 21, 2014 at 16:05
  • Edit your question to include the altered code-- the code using the post status hook. And check the post marked as a duplicate by toscho.
    – s_ha_dum
    Commented Apr 21, 2014 at 16:30
  • I found the answer thanks. I appreciate you guys pointing me in the right direction, this was useful as well: codex.wordpress.org/Plugin_API/Action_Reference/publish_post. How do I post my solution? Do I just edit the question? Sorry about the duplicate but I was a bit stuck.
    – Chris
    Commented Apr 21, 2014 at 16:42
  • If you have a solution you wish to post you can, and should, post an answer in the "Answer" box just like any other user.
    – s_ha_dum
    Commented Apr 21, 2014 at 17:12

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