返回介绍

wp_transition_post_status()

发布于 2017-09-11 13:00:28 字数 4828 浏览 1013 评论 0 收藏 0

wp_transition_post_status( string $new_status,  string $old_status,  WP_Post $post )

Fires actions 相关函数 to the transitioning of a post’s status.


description

When a post is saved, the post status is "transitioned" from one status to another, though this does not always mean the status has actually changed before and after the save. This function fires a number of action hooks 相关函数 to that transition: the generic ‘transition_post_status’ action, as well as the dynamic hooks ‘$old_status_to_$new_status’ and ‘$new_status_$post->post_type’. Note that the function does not transition the post object in the database.

For instance: When publishing a post for the first time, the post status may transition from ‘draft’ – or some other status – to ‘publish’. However, if a post is already published and is simply being updated, the "old" and "new" statuses may both be ‘publish’ before and after the transition.


参数

$new_status

(string) (Required) Transition to this post status.

$old_status

(string) (Required) Previous post status.

$post

(WP_Post) (Required) Post data.


源代码

File: wp-includes/post.php

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.
	 *
	 * @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.
	 *
	 * 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
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	do_action( "{$new_status}_{$post->post_type}", $post->ID, $post );
}

更新日志

Versiondescription
2.3.0Introduced.

相关函数

Uses

  • wp-includes/plugin.php: do_action()
  • wp-includes/post.php: transition_post_status
  • wp-includes/post.php: {$old_status}_to_{$new_status}
  • wp-includes/post.php: {$new_status}_{$post->post_type}

Used By

  • wp-includes/theme.php: _wp_customize_publish_changeset()
  • wp-includes/post.php: wp_publish_post()
  • wp-includes/post.php: wp_insert_post()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    Basic Example

    The following example is based on wp_publish_post().

    
    global $wpdb;
    
    if ( ! $post = get_post( $post ) ) return;
    
    if ( 'publish' == $post->post_status ) return;
    	
    $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) );
    
    clean_post_cache( $post->ID );
    	
    $old_status = $post->post_status;
    $post->post_status = 'publish';
    wp_transition_post_status( 'publish', $old_status, $post );
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文