do_action( 'transition_post_status', string $new_status, string $old_status, WP_Post $post )

Fires when a post is transitioned from one status to another.


Description Description


Parameters Parameters

$new_status

(string) New post status.

$old_status

(string) Old post status.

$post

(WP_Post) Post object.


Top ↑

Source Source

File: wp-includes/post.php

View on Trac


Top ↑

Changelog Changelog

Changelog
Version Description
2.3.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Drew Jaynes

    Sometimes you only want to fire a callback when a post status is transitioned to ‘publish’, i.e. coming from some other status other than publish (published posts retain the publish status even when updating).

    Only fire a callback when a post status is transitioned to publish

    /**
     * Fire a callback only when my-custom-post-type posts are transitioned to 'publish'.
     *
     * @param string  $new_status New post status.
     * @param string  $old_status Old post status.
     * @param WP_Post $post       Post object.
     */
    function wpdocs_run_on_publish_only( $new_status, $old_status, $post ) {
    	if ( ( 'publish' === $new_status && 'publish' !== $old_status )
    		&& 'my-custom-post-type' === $post->post_type
    	) {
    			// do stuff
    	}
    }
    add_action( 'transition_post_status', 'wpdocs_run_on_publish_only', 10, 3 );
    
  2. Skip to note 2 content
    Contributed by Ashar Irfan

    Example: Check if someone published a custom post type first time.

    // Check to see if user posted first time.
    add_action( 'transition_post_status', 'some_function', 10, 3 );
    function some_function( $new, $old, $post ) {
        if ( ( $new == 'publish' ) && ( $old != 'publish' ) && ( $post->post_type == 'your_post_type' ) ) {
    		// do stuff
    	} else {
    		return;
    	}
    }
    
  3. Skip to note 3 content
    Contributed by RavanH

    Please note that the name transition_post_status is misleading. The hook does not only fire on a post status transition but also when a post is updated while the status is not changed from one to another at all.

    So if you wish to really only do stuff on status transition and not on regular post updates, you will need to (at least) start with a basic bailout like this:

    /**
     * Do stuff only when posts are actually transitioned from one status to another.
     *
     * @param string  $new_status New post status.
     * @param string  $old_status Old post status.
     * @param WP_Post $post       Post object.
     */
    function wpdocs_run_on_transition_only( $new_status, $old_status, $post ) {
        if ( $old_status == $new_status )
    		return;
    
    	// do stuff
    }
    add_action( 'transition_post_status', 'wpdocs_run_on_transition_only', 10, 3 );
    

    For doing stuff when moving in and out (!) of published status only, use

    if ( $old_status == $new_status || $old_status != 'publish' && $new_status != 'publish' )
    	return;
    
    // do stuff
    

You must log in before being able to contribute a note or feedback.