I needed an action to monitor for posts being published, unpublished or updated after they were already published in my WordPress plugin. I discovered Post_Status_Transitions and specifically the
transtion_post_status action that is called for every change to a post’s status. I then check if either the
$old_status or the
$new_status variables are ‘
publish‘ and then I know the article is either being published or being unpublished.
An important note when you are implementing the
When you call
add_action, you need to modify the
$accepted_args parameter. The default value is 1, it needs to be changed to 3.
transition_post_status (as defined at http://codex.wordpress.org/Post_Status_Transitions) sends three parameters,
If you only write
add_action('transition_post_status', 'my_function'), the default number of parameters that are sent to
my_function is 1 so you will only get the
$new_status parameter and the other two params won’t be sent.
The correct syntax to add a transition_post_status action is:
add_action('transition_post_status', 'my_function', 10, 3); my_function will then be called with all three parameters (
$new_status, $old_status, $post).
Some good reference posts if you’re working with this action: