0

I have a custom post type called "documents". I can add documents (ACF repeater field) there and the users can see them in a frontend-view. Now i need to send an email-notification to the author of the post when i change the custom-post-type in the backend and save it.

In another project i worked with the following function in the functions.php file, but this code is just working when i create a new post. I don't need to send mails on creating a new post but when i save them again.

/* SEND MAIL WHEN CPT PRODUKTIONSAUFTRAG ERSTELLT */
add_action('future_to_publish', 'send_emails_on_new_event');
add_action('new_to_publish', 'send_emails_on_new_event');
add_action('draft_to_publish' ,'send_emails_on_new_event');
add_action('auto-draft_to_publish' ,'send_emails_on_new_event');

function send_emails_on_new_event($post) {
    $emails = "info@mymail.com";
    $headers = 'From: Name <info@mymail.com>';
    $title = wp_strip_all_tags(get_the_title($post->ID));
    $message = "New post created";
    if(get_post_type($post->ID) === 'documents') {
        wp_mail($emails, "New Post", $message, $headers);
    } 
}
0

Try this way:

function send_emails_on_new_event( $post ) {

    $emails  = 'info@mymail.com';
    $headers = 'From: Name <info@mymail.com>';
    $title   = wp_strip_all_tags( get_the_title( $post->ID ) );
    $message = 'New post created';

    if ( get_post_type( $post->ID ) === 'documents' ) {
        wp_mail( $emails, 'New Post', $message, $headers );
    }
}
add_action( 'pre_post_update', 'send_emails_on_new_event' );

From the Code Reference:

pre_post_update: Fires immediately before an existing post is updated in the database.

  • Thank you. But this function is also sending a mail when a post is saved / for the first time. Im looking for a solution when the email is just sent when an existing post is updatet. – Peesen87 Sep 21 '18 at 13:41
  • @Peesen87 this function with pre_post_update send an email every time the post (cpt: documents) is updated. – benny-ben Sep 21 '18 at 14:11
  • @Peesen87 did you manage to make it work? – benny-ben Sep 23 '18 at 7:50

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.