I'm trying to use get_post_meta
with the following hook, but it's always empty. I adjusted the priority to see if that would help, but no luck. Any ideas?
The post meta is definitely in the db after publish.
function some_function( $new_status, $old_status, $post ) {
if ( ( $new_status == 'publish' ) && ( $old_status != 'publish' ) && ( $post->post_type == 'cpt' ) ) {
// send admin notification
$post_title = get_the_title( $post->ID );
$message .= 'Name: ' . $post_title . '<br>';
$message .= 'Email: ' . get_post_meta($post->ID, '_email', true) . '<br>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: ' . get_option( 'blogname' ) . ' <' . get_option( 'admin_email' ) . '>';
wp_mail( get_option( 'admin_email' ), $subject, $message, $headers );
} else {
return;
}
}
add_action( 'transition_post_status', 'some_function', 100, 3 );
The post_title
is coming across in the email using the above, just no meta data.
If there's another hook or easier way, I'm all ears!
I'm trying to use get_post_meta
with the following hook, but it's always empty. I adjusted the priority to see if that would help, but no luck. Any ideas?
The post meta is definitely in the db after publish.
function some_function( $new_status, $old_status, $post ) {
if ( ( $new_status == 'publish' ) && ( $old_status != 'publish' ) && ( $post->post_type == 'cpt' ) ) {
// send admin notification
$post_title = get_the_title( $post->ID );
$message .= 'Name: ' . $post_title . '<br>';
$message .= 'Email: ' . get_post_meta($post->ID, '_email', true) . '<br>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: ' . get_option( 'blogname' ) . ' <' . get_option( 'admin_email' ) . '>';
wp_mail( get_option( 'admin_email' ), $subject, $message, $headers );
} else {
return;
}
}
add_action( 'transition_post_status', 'some_function', 100, 3 );
The post_title
is coming across in the email using the above, just no meta data.
If there's another hook or easier way, I'm all ears!
Share Improve this question asked Oct 22, 2019 at 22:30 Dario ZadroDario Zadro 4895 silver badges10 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0I got it working by looking at this post/answer: https://wordpress.stackexchange/a/134283/17126
Seems it can't be done, and I changed the get_post_meta
line to:
$message .= 'Email: ' . sanitize_text_field($_POST['_email']) . '<br>';
Also, I could see the $_POST
data coming in from post.php on submission over dev tools.
And finally, I changed my hook to publish_cpt
which you can read more about here:
https://codex.wordpress/Post_Status_Transitions
All good now!
get_post_meta
? I see you don't check the return value for errors – Tom J Nowell ♦ Commented Oct 22, 2019 at 22:52get_post_meta
is blank – Dario Zadro Commented Oct 22, 2019 at 22:54