if && problems with transition_post_status

Your original function relies on the $_POST['post_type'] being set to the appropriate value. As a general rule, you should avoid using global variables – if you use only want the function gives to you, you don’t have to think about the contexts in what it should be called.

In this case, that’s what’s happened. You’re function relies on a global variable $_POST['post_type'], and while that works in one ‘state’ (publishing a post) it doesn’t in another (a cron job, updating a post). In short, $_POST['post_type'] isn’t always what you think it should be.

The following retrieves the post type from the passed $post variable:

function intercept_all_status_changes( $new_status, $old_status, $post ) {
    if ( $new_status == 'publish' && get_post_type( $post ) == 'tweet' ) {
        // Post status changed
        twitter_run_when_published ();
    }
}  
add_action( 'transition_post_status', 'intercept_all_status_changes', 10, 3 );

Leave a Comment