最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - Hook after all metadata are set is triggering several time

programmeradmin1浏览0评论

I need to trigger a function everytime the user save the post manually (so only thanks to the "Update button").

Here's what I have :

add_action( 'save_post', 'my_save_post_function', 10, 3 );

function my_save_post_function($object_id, $post, $updated)  {

    $post = get_post($object_id);
    $post_ID = $object_id;

    if(!wp_doing_ajax() && count($_REQUEST) > 2){

        if($post->post_type == "incsub_event" && $_POST["action"] = "editpost")
  { /* do stuff*/ }
}

Problem : this function is trigger twice (I can see that thanks to my code in do stuff, everything is dupplicated).

Seems like the first time is triggered when the user click on the link, and the second time (maybe ?) after the metadata are updated.

How can I trigger this only once ?

PS : tried with hook "post_updated" (doesn't update my metadata) and "update_post_metadata" (triggers twice too)

In my case, I'm using ACF.

ACF provides a hook that does exactly what I want, but only for ACF fields :

add_action( 'acf/save_post', 'my_save_post_function', 15 );

function my_save_post_function($object_id)  { /* do stuff */ }

I need to trigger a function everytime the user save the post manually (so only thanks to the "Update button").

Here's what I have :

add_action( 'save_post', 'my_save_post_function', 10, 3 );

function my_save_post_function($object_id, $post, $updated)  {

    $post = get_post($object_id);
    $post_ID = $object_id;

    if(!wp_doing_ajax() && count($_REQUEST) > 2){

        if($post->post_type == "incsub_event" && $_POST["action"] = "editpost")
  { /* do stuff*/ }
}

Problem : this function is trigger twice (I can see that thanks to my code in do stuff, everything is dupplicated).

Seems like the first time is triggered when the user click on the link, and the second time (maybe ?) after the metadata are updated.

How can I trigger this only once ?

PS : tried with hook "post_updated" (doesn't update my metadata) and "update_post_metadata" (triggers twice too)

In my case, I'm using ACF.

ACF provides a hook that does exactly what I want, but only for ACF fields :

add_action( 'acf/save_post', 'my_save_post_function', 15 );

function my_save_post_function($object_id)  { /* do stuff */ }
Share Improve this question edited Aug 16, 2019 at 9:09 Morgan asked Aug 16, 2019 at 8:47 MorganMorgan 13710 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Add remove_action( 'save_post', 'my_save_post_function', 10, 3 ); inside your my_save_post_function before doing any update.

发布评论

评论列表(0)

  1. 暂无评论