该钩子在更新帖子后被激发。当邮件被发送到垃圾箱时,从技术上讲是post_status
更改,因此这是一个更新,因此钩子被触发。
当你想改变职位状态时,最好的选择是post status transitions hooks, e、 g.针对垃圾桶后的使用:
add_action( \'transition_post_status\', \'fired_on_post_change_staus\', 10, 3 );
function fired_on_post_change_staus( $new_status, $old_status, $post ) {
if ( $new_status == \'trash\' ) {
// Do something when post is deleted
}
}
但是,更新帖子时,帖子状态可以保持不变,在这种情况下,您可以使用
edit_post
立柱挂钩。在执行更新并将post对象作为第二个参数传递后,一旦此钩子被触发,您就可以检查实际的post状态,例如。
add_action( \'edit_post\', \'fired_on_post_edit\', 10, 2 );
function fired_on_post_edit( $post_ID, $post ) {
if ( $post->post_status != \'trash\' ) {
// Do something when post is updated but not deleted
}
}
可以使用
post_updated
hook,这与前面的类似,但传递更新之前的post如何以及更新之后的post如何,例如。
add_action( \'post_updated\', \'fired_on_post_edit_2\', 10, 3 );
function fired_on_post_edit_2( $post_ID, $post_after, $post_before ) {
if ( $post_after->post_status == \'trash\' && $post_before->post_status == \'publish\' ) {
// Do something when post is trashed after being published
}
if ( $post_after->post_status == \'publish\' && $post_before->post_status == \'trash\' ) {
// Do something when post is published after being trash (post undelete)
}
}