wp_transition_post_status

函数
wp_transition_post_status ( $new_status, $old_status, $post )
参数
  • (string) $new_status Transition to this post status.
    Required:
  • (string) $old_status Previous post status.
    Required:
  • (WP_Post) $post Post data.
    Required:
定义位置
相关方法
_transition_post_statuswp_transition_comment_status_update_term_count_on_transition_post_status_update_posts_count_on_transition_post_statuswp_set_post_cats
引入
2.3.0
弃用
-

wp_transition_post_status: 当一个文章的状态发生变化时,这个函数会触发动作,比如从草稿到发表。

触发与文章状态转换有关的动作。

当一个文章被保存时,文章的状态被从一个状态”过渡”到另一个状态,尽管这并不总是意味着状态在保存前后实际发生了变化: 这个函数触发了一些与过渡有关的动作钩子:一般的{@see ‘transition_post_status’}动作,以及动态钩子{@see ‘$old_status_to_$new_status’}和{@see ‘$new_status_$post->post_type’}。注意,该函数并不过渡数据库中的文章对象。

比如说: 当第一次发布一个文章时,文章的状态可能从”草稿”–或其他状态–过渡到”发布”。然而,如果一个文章已经发布了,只是被更新了,”旧”和”新”状态在转换前后都可能是”发布”。

function wp_transition_post_status( $new_status, $old_status, $post ) {
	/**
	 * Fires when a post is transitioned from one status to another.
	 *
	 * @since 2.3.0
	 *
	 * @param string  $new_status New post status.
	 * @param string  $old_status Old post status.
	 * @param WP_Post $post       Post object.
	 */
	do_action( 'transition_post_status', $new_status, $old_status, $post );

	/**
	 * Fires when a post is transitioned from one status to another.
	 *
	 * The dynamic portions of the hook name, `$new_status` and `$old_status`,
	 * refer to the old and new post statuses, respectively.
	 *
	 * Possible hook names include:
	 *
	 *  - `draft_to_publish`
	 *  - `publish_to_trash`
	 *  - `pending_to_draft`
	 *
	 * @since 2.3.0
	 *
	 * @param WP_Post $post Post object.
	 */
	do_action( "{$old_status}_to_{$new_status}", $post );

	/**
	 * Fires when a post is transitioned from one status to another.
	 *
	 * The dynamic portions of the hook name, `$new_status` and `$post->post_type`,
	 * refer to the new post status and post type, respectively.
	 *
	 * Possible hook names include:
	 *
	 *  - `draft_post`
	 *  - `future_post`
	 *  - `pending_post`
	 *  - `private_post`
	 *  - `publish_post`
	 *  - `trash_post`
	 *  - `draft_page`
	 *  - `future_page`
	 *  - `pending_page`
	 *  - `private_page`
	 *  - `publish_page`
	 *  - `trash_page`
	 *  - `publish_attachment`
	 *  - `trash_attachment`
	 *
	 * Please note: When this action is hooked using a particular post status (like
	 * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is
	 * first transitioned to that status from something else, as well as upon
	 * subsequent post updates (old and new status are both the same).
	 *
	 * Therefore, if you are looking to only fire a callback when a post is first
	 * transitioned to a status, use the {@see 'transition_post_status'} hook instead.
	 *
	 * @since 2.3.0
	 * @since 5.9.0 Added `$old_status` parameter.
	 *
	 * @param int     $post_id    Post ID.
	 * @param WP_Post $post       Post object.
	 * @param string  $old_status Old post status.
	 */
	do_action( "{$new_status}_{$post->post_type}", $post->ID, $post, $old_status );
}

常见问题

FAQs
查看更多 >