do_action( “{$new_status}_{$post->post_type}”, int $post_id, WP_Post $post, string $old_status )

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

Description

The dynamic portions of the hook name, $new_status and $post->post_type, refer to the new post status and post type, respectively.

Possible hook names include:

  • draft_post
  • future_post
  • pending_post
  • private_post
  • publish_post
  • trash_post
  • draft_page
  • future_page
  • pending_page
  • private_page
  • publish_page
  • trash_page
  • publish_attachment
  • trash_attachment

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 ‘transition_post_status’ hook instead.

Parameters

$post_idint
Post ID.
$postWP_Post
Post object.
$old_statusstring
Old post status.

Source

do_action( "{$new_status}_{$post->post_type}", $post->ID, $post, $old_status );

Changelog

VersionDescription
5.9.0Added $old_status parameter.
2.3.0Introduced.

User Contributed Notes

  1. Skip to note 5 content

    Example Migrated from Codex:

    The example below will send an email via wp_mail() to the post author when their article is published.

    function post_published_notification( $post_id, $post ) {
        $author = $post->post_author; /* Post author ID. */
        $name = get_the_author_meta( 'display_name', $author );
        $email = get_the_author_meta( 'user_email', $author );
        $title = $post->post_title;
        $permalink = get_permalink( $post_id );
        $edit = get_edit_post_link( $post_id, '' );
        $to[] = sprintf( '%s <%s>', $name, $email );
        $subject = sprintf( 'Published: %s', $title );
        $message = sprintf ('Congratulations, %s! Your article "%s" has been published.' . "\n\n", $name, $title );
        $message .= sprintf( 'View: %s', $permalink );
        $headers[] = '';
        wp_mail( $to, $subject, $message, $headers );
    }
    add_action( 'publish_post', 'post_published_notification', 10, 2 );
  2. Skip to note 6 content
    add_action('publish_post', 'send_notification');
    
    function send_notification($id, $post_obj){
    	
    	//Assemble the message
    	$msg = 'Hi, a new post has been published. Here is the content:';
    	$msg .= $post_obj-&gt;post_content;
    	
    	//Send the notification
    	wp_mail('recipient@example.com', $post_obj-&gt;post_title, $msg);
    }
  3. Skip to note 7 content

    This WP hook helps you receive a slack notification every time a user saves a new post on your site. It uses your Slack webhook, username, and channel. It makes use of the PHP Slack library which can be downloaded via Composer to your themes directory.

    function wpdocs_notify_my_slack( $post_id, $post ) {
    	require_once __DIR__ . '/vendor/autoload.php';
    
    	$slack_hook = 'https://hooks.slack.com/services/xxxxxx';
    	$slack_message = 'New Post alert | %3$s: %2$s - %1$s';
    	$slack_message = sprintf( $slack_message, get_permalink( $post_id ), $post->post_title, $post->post_date );
    	$settings = [
    		'username' => 'Chigozie Orunta',
    		'channel'  => '#blog',
    	];
    	$client = new MaknzSlackClient( $slack_hook, $settings );
    	$client->send( $slack_message );
    }
    add_action( 'publish_post', 'wpdocs_notify_my_slack', 10, 2 );
  4. Skip to note 8 content

    This example helps you log a published post to a text file in your (child) theme.

    add_action( 'publish_post', 'wpdocs_log_published_post' );
    
    function wpdocs_log_published_post( $post_id ) {
        $log_file = trailingslashit( get_stylesheet_directory() ) . apply_filters( 'wpdocs_published_log_file', 'log.txt' );
    
        if ( ! file_exists( $log_file ) ) {
            return;
        }
    
        $file = fopen( $log_file, 'a' );
    
        if ( false === $file ) {
            return;
        }
    
        $message = sprintf( '%s (%d) was just saved.', get_the_title( $post_id ), $post_id );
        fwrite( $log_file, $message . PHP_EOL );
    }

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