Future_to_publish into postmeta

Based on an example found on this page, I attempted to use this in my plugin. Nothing fired. A note on this page stated publish_future_post is deprecated and replaced with future_to_publish. This also failed.

Therefore, I tried to make a simple plugin with only this add_action into the plugin. Once again, the postmeta was not updated.

Here is the code in the “future post” plugin.

function future_publish ( $post_id ) {
    update_post_meta( $post_id, 'hook_fired', 'true' );
}

add_action( 'future_to_publish', 'future_publish' );

I’ve been going round and round. Some people are posting the transition status works fine, others say it cannot be wrapped in is_admin, and still, I cannot get a scheduled post to actually fire anything except for the post to WP. I cannot hook into it.

Has anyone actually tried to hook in and had something happen after the scheduled post becomes published?

Update: Please excuse my typing. I meant published and originally typed post. It’s due to my frustration with this topic.

Update Number 2: I copied the code from an answer which is specific to $old == ‘future’ and this worked to update the meta. Great. But the code after the meta (not shown here) doesn’t run. At least now I know that the action is working when $old == ‘future’. I’m not sure why the rest of the code after doesn’t work but that would be a different question.

For now, I can only assume that the code $old != ‘publish’ does not fire scheduled posts and the ‘future’ must be specified. I’ll mark the answer.

2 Answers
2

Use publish_future_post action hook. Contrary to what Codex says, it is not deprecated, and it works with WordPress 4.8.2. Your code should be:

function my_test_future_post( $post_id ) {
    update_post_meta( $post_id, 'hook_fired', 'true' );
}
add_action( 'publish_future_post', 'my_test_future_post' );

Tested!

Update

If you are concerned about publish_future_post hook being deprecated, use transition_post_status hook:

function my_test_future_post( $new, $old, $post ) {
    if ( $post->post_type == 'post' && $new == 'publish' && $old == 'future' )
        update_post_meta( $post->ID, 'hook_fired', 'You bet!' );
}
add_action( 'transition_post_status', 'my_test_future_post', 10, 3 );

Tested in functions.php and plugins.

Leave a Comment