admin管理员组文章数量:1415697
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
1 Answer
Reset to default 0Add remove_action( 'save_post', 'my_save_post_function', 10, 3 );
inside your my_save_post_function
before doing any update.
本文标签: ajaxHook after all metadata are set is triggering several time
版权声明:本文标题:ajax - Hook after all metadata are set is triggering several time 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745237822a2649141.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论