Josh Schumacher: A PHP Developer

The biggest and the best in the Northwest

Josh Schumacher: A PHP Developer random header image

WordPress: transition_post_status action

October 11th, 2011 · No Comments

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 transition_post_status action

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.

The action transition_post_status (as defined at http://codex.wordpress.org/Post_Status_Transitions) sends three parameters, $new_status, $old_status, $post.

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:

Tags: Uncategorized

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment