Post Meta in Notification Email for New Posts
-
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 );
- The topic ‘Post Meta in Notification Email for New Posts’ is closed to new replies.