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.
Contents
Parameters
-
$new_status
string -
New post status.
-
$old_status
string -
Old post status.
-
$post
WP_Post -
Post object.
Source
File: wp-includes/post.php
.
View all references
do_action( 'transition_post_status', $new_status, $old_status, $post );
Changelog
Version | Description |
---|---|
2.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
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:
For doing stuff when moving in and out (!) of published status only, use
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
Example: Check if someone published a custom post type first time.
Top ↑
Feedback
This does not guarantee that it is the first time the post was published: it will fire if the post was published, changed to a different status, and then published again. — By crstauf —
If you are looking to hook something on a specific post status change you can also use this hook
This could be used for two specific post status. For example if you are looking to hook something when a draft post is published you can use this hook like this,
Of course this opens up a possibility for so many things and you don’t have to keep relying on
transition_post_status
which runs even when post status are not changed.Top ↑
Feedback
It’s important to note that using anonymous functions in add_action() or add_filter() is in most cases an incredibly bad idea. This would make the code in the functions really hard to get rid of from another plugin or theme. In cases when your code is to be used in other people’s projects, anonymous functions are almost “immune” to being removed through remove_function() or remove_filter() and it’s rarely the case that one would look after this effect. — By Vladimir Vassilev —
Status can be one of: publish, future, draft, pending, private
Top ↑
Feedback
If there are plugins that introduce new status types, they may also be (untested) run through this hook. — By crstauf —
This seems to be incomplete. List from old codex https://codex.wordpress.org/Post_Status_Transitions, section with title “The available post statuses are:” — By te-online —