1

I'm attempting to get this function to work and it seems to not work when put together. When I just have

 $new_status == 'publish' 

it works, when I use just the second it also works. When I attempt to && them together there seems to be a hiccup.

Also it should be noted that I attempted just to use the

add_action ('publish_tweet', 'twitter_run_when_published');

but that also didn't work.

Here is the full function that I'm attempting to use, it is from a codex example

function intercept_all_status_changes( $new_status, $old_status, $post ) {
  if ( $new_status == 'publish' && $_POST['post_type'] == 'tweet' ) {
    // Post status changed

  twitter_run_when_published ();

  }
}
  add_action( 'transition_post_status', 'intercept_all_status_changes', 10, 3 );

The twitter_run_when_published works. I know this because individually the if/condition works.

Some quirks: The function will work if I publish immediately. It does not work when I schedule a future post.

Additional info that might help I attempted these in the code, they did not work.

add_action('publish_tweet', 'twitter_run_when_published');
//possible alts to make it happen only once and not double down

add_action('new_to_publish_tweet', 'twitter_run_when_published');
add_action('draft_to_publish_tweet', 'twitter_run_when_published');
add_action('pending_to_publish_tweet', 'twitter_run_when_published');
add_action('future_to_publish_tweet', 'twitter_run_when_published');
add_action('auto-draft_to_publish_tweet', 'twitter_run_when_published');
add_action('private_to_publish_tweet', 'twitter_run_when_published');
add_action('inherit_to_publish_tweet', 'twitter_run_when_published');
add_action('trash_to_publish_tweet', 'twitter_run_when_published');

Only publishing immediately performs the function mentioned above.

The classic example works.

function intercept_all_status_changes( $new_status, $old_status, $post ) {
  if ( $new_status != $old_status) {
    // Post status changed

    twitter_run_when_published ();

 }
}
add_action( 'transition_post_status', 'intercept_all_status_changes', 10, 3 );

But this will fire off the function multiple times. It doesn't work in the plugin but will work for sure in the theme.

What am I missing? I'd like to have it so that when an article is published and it meets the custom post type tweet that it performs a function.

EDIT: I was rambling a bit and attempted to clear up a few poorly phrased sentences.

4

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 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.