Save post action is called twice

Question

I’ve seen this posted several times with no perfect answer:

I have an action hook for save_post on a CPT. I’ve tried everything I could find but it always gets called twice:

Here’s my code:

function mySavePostCustomFunc( $post_id, $post, $update ) {
 
     // If this is just a revision, or if it is an autosave

    if ( wp_is_post_revision( $post_id) || wp_is_post_autosave( $post_id ) )  {
        return;
    }


     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
            return;
    }

    // don't run the echo if the function is called for saving revision.
    if ( $post->post_type == 'revision' ) {
        return;
    }


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


     if (get_post_type($post_id) != 'my_post_type') {
        return;
     }
 
    // this gets called twice
    error_log("post save function was called");


     $curDateTime = date('Y-m-d H:i:s');
    

     // I've tried commenting this out also
     update_post_meta( $post_id, 'update_time_meta_field', $curDateTime);


}

add_action( 'save_post_my_post_type', 'mySavePostCustomFunc', 10, 3 );

Thanks

0
Best Dev Tutorials 2 months 0 Answers 16 views 0

Leave an answer