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 */ }

1 Answer 1

0

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

3
  • Nop sorry, the code is executed like 80 times ....
    – Morgan
    Commented Aug 16, 2019 at 9:32
  • Can you clarify what you are trying to achieve? Are you trying to add changes after the button Update being clicked? Commented Aug 16, 2019 at 9:35
  • On update click, I want to fill a file with the data of the post (data AND metadata)
    – Morgan
    Commented Aug 16, 2019 at 9:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.