I am using save_post
for a function to send an email when a post is updated by a user. This is firing twice and I am aware this is due to the post revisions and autosaves.
I have tried to prevent this from happening by wrapping my wp_mail
within a conditional statement but this still fires twice. What adjustments do I need to make to ensure this only fires once when a user updates the post?
function updated_search_notification($post_id)
{
$post_type = get_post_type($post_id);
if ($post_type === 'utility-search') {
if ((wp_is_post_revision($post_id)) || (wp_is_post_autosave($post_id))) {
// post is autosave
} else {
// Message Variables
$siteurl = get_option('siteurl');
$post_url = '' . $siteurl . '/wp-admin/post.php?post=' . $post_id . '&action=edit';
$new_search_name = '';
//$new_search_email = get_option( 'new_search_email' );
$new_search_email = '[email]';
$utility_search_customer = '';
$subject = 'Your search has been updated';
// Message Contents
$message = "[Message Contents]";
// Send Email
wp_mail($new_search_email, $subject, $message);
}
}
}
add_action('save_post', 'updated_search_notification', 10, 3);