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' );