Email notification for editors only

Use get_users() function. Reference

function notify_editors( $post_id ) {
    $post = get_post( $post_id );

    // Get all editors
    $editors = get_users( [ 'role__in' => [ 'editor'] ] );

    foreach ( $editors as $editor ) {

        // Setup email
        $subject = "Post Published: " . $post->post_title;
        $message=" Hi " . $editor->display_name . ',
        Your post, "' . $post->post_title . '" has just been published.
        View post: ' . get_permalink( $post_id ) . 'Thanks';

        // and send...
        wp_mail( $editor->user_email, $subject, $message );
     }
}

add_action( 'publish_post', 'notify_editors' );  

You can read more here, about optional headers parameter that can add fields like from, cc, content-type etc.

Leave a Comment