admin管理员组

文章数量:1315079

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

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

Share Improve this question edited Nov 22, 2020 at 19:05 Best Dev Tutorials asked Nov 22, 2020 at 18:29 Best Dev TutorialsBest Dev Tutorials 4451 gold badge7 silver badges21 bronze badges 9
  • Which editor are you using? Keep in mind that some plugin and theme authors don't know how to save post meta in the block editor and instead resorted to making an AJAX call that updates the post, they may be the cause, but there's not enough information to diagnose this. How are you testing this code? And how do you know how many times it runs? – Tom J Nowell Commented Nov 22, 2020 at 18:55
  • @TomJNowell I'm the plugin author and this is the only plugin installed on the site. The block editor is what's being used. I know it's being called twice because in my version I have an error_log("post save function being called") inside the function. – Best Dev Tutorials Commented Nov 22, 2020 at 19:02
  • and have you confirmed that the messages both come from the same request? How are you differentiating between the function being called twice, versus a REST API endpoint being hit in 2 separate requests? You can do this by using a counter, if the counter increments then it's the same request. The issue I mentioned above still applies even if you aren't the one making the extra requests. If you can test while verifying this happens with the default theme and no other plugins? – Tom J Nowell Commented Nov 22, 2020 at 19:06
  • Also, where in the function did you put the error_log call? Do you have any wp_insert_post or wp_update_post calls in your code? If the function is called multiple times have you confirmed if it's called the same way with the same parameters for the same post? Or are they different? – Tom J Nowell Commented Nov 22, 2020 at 19:07
  • 1 You can look in the browser dev tools network panel to see what requests are being made. Although, what you're trying to save in the hook is also the value post_modified will have – Tom J Nowell Commented Nov 22, 2020 at 19:18
 |  Show 4 more comments

1 Answer 1

Reset to default 2

I noticed there was a 302 redirect in my network tab while saving. Save_post was being triggered twice, once from

Inside my call, I simply included:

$referrer = wp_get_referer();

if (strpos($referrer, 'http:') !== false) {
    error_log("not the right referrer, aborting");
    return;
}

Issue solved.

本文标签: Save post action is called twice