2

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.

0

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.

  • While it might still work at the moment it's a big risk to use it - if the Codex says its deprecated, then it could be removed at any time and break the functionality. It would be preferable to get it working from the start with functions that are confirmed to be part of the core, to be sure :) – FluffyKitten Oct 1 '17 at 9:03
  • @FluffyKitten I've updated my answer, to address your concerns. – Frank P. Walentynowicz Oct 1 '17 at 10:18
  • Interesting because that isn’t working on my test environment. – LPH Oct 1 '17 at 14:56
  • The key was using a different conditional in which $old == 'future' rather than using $old != 'publish' – LPH Oct 1 '17 at 16:25
1

Update:

Now that you've changed your question (and made my original answer look completely wrong!):

I haven't used the hook future_to_publish, but I have successfully used the transition_post_status action (also referenced in the Codex page) to detect status changes.

Something similar to this has worked for me before:

add_action( 'transition_post_status', 'my_status_change', 10, 3 );

function my_status_change( $new_status, $old_status, $post ) {
    if ($old_status=="future" && $new_status=="publish"){
        // do stuff....
    }
}

I think you might have mixed up 2 hooks? The Codex refers to the following hooks:

  1. {old_status}_to _{new_status} Hook

An {old_status}to{new_status} action will execute when a post transitions from {old_status} to {new_status}.

In your case: future_to_publish (future_to_post won't work because post isn't a status

  1. {status}_{post_type} Hook

A {status}_{post_type} action will execute when a post of type {post_type} transitions to {status} from any other status.

In your case: This would be publish_post (I think this may be where you got them mixed up?)

I assume you only want future to publish transitions so try:

add_action( 'future_to_publish', 'future_publish' );
  • No hook I've tried has worked. In the example in this question, I'm showing code which postings in other locations shows should add to postmeta. However, this fails. Have you tried to get something to fire after a scheduled post? – LPH Oct 1 '17 at 7:01
  • I'm sorry, I didn't notice my typing and indeed meant future_to_publish. Regardless, my question is one of exasperation. Has anyone successfully hooked into scheduled posts? – LPH Oct 1 '17 at 7:06
  • @LPH yes, my updated answer has tested code. – Frank P. Walentynowicz Oct 1 '17 at 10:23
  • @FrankP.Walentynowicz So does mine :) – FluffyKitten Oct 1 '17 at 10:25
  • @LPH So does mine :) As I say in my updated answer, transition_post_status has always worked for me. – FluffyKitten Oct 1 '17 at 10:26

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.