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.
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.
Share Improve this question asked May 4, 2014 at 20:10 CodyCody 473 silver badges8 bronze badges2 Answers
Reset to default 4Your 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 );
I've had this problem also, even while avoiding $_POST[]
This hook appears to be overall quite buggy.
You can see from this GitHub thread a discussion about how the transition_post_status hook gets called twice for every post status update: https://github/WordPress/gutenberg/issues/15094
The problems I faced were:
- if/conditions not working within the function (as outlined by questioner above)
- the transition_post_status hook getting called twice
My suggestion is to avoid this hook if you can.