Action hook '{$new_status}_{$post->post_type}'

in WP Core File wp-includes/post.php at line 5798

View Source

{$new_status}_{$post->post_type}

Action Hook
Description
Fires when a post is transitioned from one status to another. 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 {@see 'transition_post_status'} hook instead.

Hook Information

File Location wp-includes/post.php View on GitHub
Hook Type Action
Line Number 5798

Hook Parameters

Type Name Description
int $post_id Post ID.
WP_Post $post Post object.
string $old_status Old post status.

Usage Examples

Basic Usage
<?php
// Hook into {$new_status}_{$post->post_type}
add_action('{$new_status}_{$post->post_type}', 'my_custom_function', 10, 3);

function my_custom_function($post_id, $post, $old_status) {
    // Your custom code here
}

Source Code Context

wp-includes/post.php:5798 - How this hook is used in WordPress core
<?php
5793  	 *
5794  	 * @param int     $post_id    Post ID.
5795  	 * @param WP_Post $post       Post object.
5796  	 * @param string  $old_status Old post status.
5797  	 */
5798  	do_action( "{$new_status}_{$post->post_type}", $post->ID, $post, $old_status );
5799  }
5800  
5801  /**
5802   * Fires actions after a post, its terms and meta data has been saved.
5803   *

PHP Documentation

<?php
/**
	 * Fires when a post is transitioned from one status to another.
	 *
	 * 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 {@see 'transition_post_status'} hook instead.
	 *
	 * @since 2.3.0
	 * @since 5.9.0 Added `$old_status` parameter.
	 *
	 * @param int     $post_id    Post ID.
	 * @param WP_Post $post       Post object.
	 * @param string  $old_status Old post status.
	 */
Quick Info
  • Hook Type: Action
  • Parameters: 3
  • File: wp-includes/post.php
Related Hooks

Related hooks will be displayed here in future updates.