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 | Show 4 more comments1 Answer
Reset to default 2I 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
版权声明:本文标题:Save post action is called twice 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741972024a2407898.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
error_log
call? Do you have anywp_insert_post
orwp_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:07post_modified
will have – Tom J Nowell ♦ Commented Nov 22, 2020 at 19:18