WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp transition post status

Contents

Description

Performs post status transition actions.

This function contains do_action() calls for post status transition action hooks. The order of the words in the function name might be confusing – it does not change the status of posts, it only calls actions that can be hooked into by plugin developers. Although this function is already called where needed by core functions, it may be useful when a plugin updates a post by directly interacting with the database, thereby bypassing the usual post status transition actions. For the actions to be effective, the new status, old status and post object must be passed.

To transition the status of a post, rather than perform actions when a post status is transitioned, use wp_update_post() or wp_publish_post().

Usage

<?php wp_transition_post_status$new_status$old_status$post ?>

Parameters

<tt>$new_status
(string) (required) New post status after an update.
Default: None
$old_status
(string) (required) Previous post status.
Default: None
$post
(object) (required) Post object.
Default: None

Return Values

(void) 
This function does not return a value.

Examples

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 );

Notes

  • This function is already called where needed in core functions. You do not need to call this function when changing a post’s status with wp_update_post(), for example. You do need to call this function in your plugin or theme when manually updating the status of a post, as shown in the example above.

Source Code

wp_transition_post_status() is located in wp-includes/post.php

Change Log

Related

Post Status Transitions - More information on how to hook into the actions called by this function.