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

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