On transition_post_status, notify author on publish

Support General On transition_post_status, notify author on publishResolved

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #41202
    ShemzoneShemzone
    Participant

    Hello to all
    I have a function that sends a notification to the author of an article once their article is published using the transition_post_status hook.
    I have no problem with the notification itself using the transition_post_status hook but I can't get the value of the fields I want to display in the message sent.

    Here is my function:

    add_action( 'transition_post_status', 'notify_author_on_publish', 20, 3 );
    function notify_author_on_publish( $new_status, $old_status, $post_id ) {
        $post = get_post($post_id);
        // Get the post data.
        $author_email = rwmb_meta( "author_email", "", $post_id );
        $firstname = rwmb_meta( "firstname", "", $post_id );
        if ( 'publish' !== $new_status ||
            $new_status === $old_status ||
            'my-custom_post-type' !== get_post_type( $post ) ) {
            return;
        }
    $subject = "Hi " . $firstname. ", your post has been approved: " . $post->post_title . "";
    $body = "Hi " . $firstname. "and so on...";
    
    wp_mail( '[email protected]', $subject, $body ); //Issue: cannot get $author_email value.

    Thanks a lot for your help

    #41210
    ShemzoneShemzone
    Participant

    [SOLVED]
    I had to use

     function notify_author_on_publish( $new_status, $old_status, $post )...
     $author_email = rwmb_meta( "author_email", "", $post->ID );
     $firstname = rwmb_meta( "firstname", "", $post->ID );

    instead of

     function notify_author_on_publish( $new_status, $old_status, $post_id )...
     $author_email = rwmb_meta( "author_email", "", $post_id );
     $firstname = rwmb_meta( "firstname", "", $post_id );
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.