I want to send a notification email to Author when a Custom Meta Field is added to their WordPress Post by add_post_meta or update_post_meta .
So far my code below is working successfully but it executes only when I use Save Post
function order_update_send_email( $post_id ) {
$email_sent = get_post_meta($post_id, 'email_sent', true);
$report = get_post_meta($post_id, 'report', true);
if ( $email_sent == 'Sent' ) {
return;
}
if ( $report ) {
$post_title = get_the_title( $post_id );
$post_url = get_permalink( $post_id );
$subject = 'Your report for: ' . $post_title;
$message = "Your order is completed\n\n";
$message .= "Report for: " . $post_title . "\n\n Link:" . $post_url;
$message .= "\n\n" . $report;
wp_mail( 'test@email.me', $subject, $message );
update_post_meta($post_id, 'email_sent', 'Sent');
}
}
add_action( 'save_post', 'order_update_send_email' );
In above code the Action hook save_post is working fine but I can't get add_post_meta or update_post_meta to work.
Anyone please advice, thanks.
you can use below sample code for this.
add_action( 'added_post_meta', 'wp_afterpostmeta', 10, 4 );
add_action( 'updated_post_meta', 'wp_afterpostmeta', 10, 4 );
function wp_afterpostmeta( $meta_id, $post_id, $meta_key, $meta_value )
{
if ( 'wp_meta_key' == $meta_key ) {
wp_do_something( $post_id, $meta_value );
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.