• thedittmer

    (@thedittmer)


    I’m trying to send a notification email when a new post is published that includes meta data from the post. When I already have a post published and I click “Update” it works correctly. But when I publish a post for the first time it leaves out the meta. I know that it’s getting the correct post ID because I tried just sending the ID in the message with the variable $ID when I published a new post and it works.

    Here is the code I’m using:

    function post_published_notification( $ID, $post ) {
    $author = $post->post_author; /* Post author ID. */
    $name = get_the_author_meta( ‘display_name’, $author );
    $email = get_the_author_meta( ‘user_email’, $author );
    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $edit = get_edit_post_link( $ID, ” );
    $to[] = sprintf( ‘%s <%s>’, $name, $email );
    $subject = sprintf( ‘Published: %s’, $title );
    $message = get_post_meta( $ID, ‘meta_key’, true );
    $message .= sprintf( ‘View: %s’, $permalink );
    $headers[] = ”;
    wp_mail( $to, $subject, $message, $headers );
    }
    add_action( ‘publish_post’, ‘post_published_notification’, 10, 2 );

Viewing 1 replies (of 1 total)
  • Moderator Marius L. J.

    (@clorith)

    Hi,

    I believe this is caused by the meta data not being saved because the publish_post action fires before the meta saving is performed.

    If you save a draft first, the meta data should be included, but that’s not an ideal solution obviously.
    What are you attempting to do in general (I see this code is the example from our Codex page), perhaps we can help you find a better approach if we knew what you were attempting to do?

    A ticket proposing a saner order exists at https://core.trac.wordpress.org/ticket/32223.

Viewing 1 replies (of 1 total)
  • The topic ‘Post Meta in Notification Email for New Posts’ is closed to new replies.