I have a CPT with ACF fields.
I’m trying to create a custom PHP snippet to send an email to the admin on a date determined by an ACF dat field.
I’m checking the data initially when the post is first posted or updated.
However, the function is triggered twice, once with the ACF date data and once without.
It seems, from research, that when saved, WP triggers a save and ACF also triggers a save (with the ACF data)
I thought about using the acf/save_post but if I don’t amend and ACF data, it doesn’t trigger.
I need this to trigger only once, so when a post is saved, how can I trigger a function that includes the ACF data (even if ACF fields not modified)?
Here’s my PHP snippet:
function webroom_send_mail_on_new_post( $post_id, $post ) {
if ($post->post_status !== 'auto-draft'){
$to = '[email protected]';
$updateDate = get_field(document_review_date_reminder_trigger);
$post_title = $post->post_title;
$postStatus = $post->post_status;
$subject = 'New Post Published ' . $post_title;
$message = 'Hi, new post is published on your website: ' . $post_title . ' To be updated: ' . $updateDate . ' post status: ' . $postStatus;
wp_mail($to, $subject, $message);
}
}
add_action( 'save_post_document', 'webroom_send_mail_on_new_post');