我已经看到过几次这样的帖子,但都没有完美的答案:
我有一个用于CPT上save\\u post的操作挂钩。我已经尝试了我能找到的一切,但它总是被调用两次:
这是我的代码:
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 );
谢谢