_transition_post_status

函数
_transition_post_status ( $new_status, $old_status, $post )
Access
Private
参数
  • (string) $new_status New post status.
    Required:
  • (string) $old_status Previous post status.
    Required:
  • (WP_Post) $post Post object.
    Required:
相关
  • wp_clear_scheduled_hook()
定义位置
相关方法
wp_transition_post_statuswp_transition_comment_statusregister_post_status_update_term_count_on_transition_post_statusget_post_status
引入
2.3.0
弃用
-

_transition_post_status: 这是一个动作钩子,当一个文章从一个状态过渡到另一个状态时被触发。它可以用来在文章发布、未发布或删除时执行动作。

用于管理未来文章过渡到发布的钩子

function _transition_post_status( $new_status, $old_status, $post ) {
	global $wpdb;

	if ( 'publish' !== $old_status && 'publish' === $new_status ) {
		// Reset GUID if transitioning to publish and it is empty.
		if ( '' === get_the_guid( $post->ID ) ) {
			$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) );
		}

		/**
		 * Fires when a post's status is transitioned from private to published.
		 *
		 * @since 1.5.0
		 * @deprecated 2.3.0 Use {@see 'private_to_publish'} instead.
		 *
		 * @param int $post_id Post ID.
		 */
		do_action_deprecated( 'private_to_published', array( $post->ID ), '2.3.0', 'private_to_publish' );
	}

	// If published posts changed clear the lastpostmodified cache.
	if ( 'publish' === $new_status || 'publish' === $old_status ) {
		foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) {
			wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' );
			wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' );
			wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' );
		}
	}

	if ( $new_status !== $old_status ) {
		wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' );
		wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' );
	}

	// Always clears the hook in case the post status bounced from future to draft.
	wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) );
}

常见问题

FAQs
查看更多 >