是否有一个钩子实际上只在帖子更新时触发,我在哪里可以在更新之前和之后获得$post\\u?
你可能在找post_updated
钩
使用情况
add_action( \'post_updated\', \'wpse_264720_post_updated\', 10, 3 );
function wpse_264720_post_updated( $post_ID, $post_after, $post_before ) {
//* Bail if this is an autosave
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
return;
}
//* Bail if this is a post revision
if( wp_is_post_revision( $post_ID ) ) {
return;
}
//* Bail if not the correct post types
if( ! in_array( $post_after->post_type, [ \'post-type-1\', \'post-type-2\' ] ) ) {
return;
}
//* If you add/update/delete posts, remove action to avoid infinite loop
remove_action( \'post_updated\', \'wpse_264720_post_updated\', 10 );
//* Do something useful after the post was updated
//* If the post was updated, remove save_post action
remove_action( \'save_post\', \'wpse_264720_save_post\', 10, 3 );
}
//* Hypothetical function hooked to save_post
add_action( \'save_post\', \'wpse_264720_save_post\', 10, 3 );
function wpse_264720_save_post( $post_id, $post, $update ) {
//* Bail if post was an update
if( $update ) {
return;
}
//* Do something useful on save_post
}