Unable to prevent function using save_post firing twice

First, you can use this hook to target only one custom type:
https://developer.wordpress.org/reference/hooks/save_post_post-post_type/

This hook (and save_post) is called the first time when you click on “new …” and then the hook is called with $update = FALSE.

Then to send e-mail only when the object is updated, you can test $update like this:

const UTILITY_SEARCH_POST_TYPE = "utility-search";


add_action("save_post_" . UTILITY_SEARCH_POST_TYPE, function ($post_ID, $post, $update) {

    if (wp_is_post_autosave($post_ID)) {
        return;
    }

    if (!$update) { // if new object
        return;
    }


    // preparing e-mail
    ...

    // sending e-mail
    wp_mail(...);


}, 10, 3);

Leave a Comment

tech