0

I am writing a plugin, part of the functionality does functions on hook: publish_to_draft

I then later want to re-publish that post and trigger specific functionality to see if it has previously been published.

i.e. draft_to_publish

Now whilst I already have functionality for this, what I can't tell is the difference between a post that is just a draft going to publish or a previously published post that has been moved back to draft.

This is important because it is used for integration and I need to run that functionality on that action.

Can I use the_date() for this?

Cheers Dan

0

As far as I can see, you won't get any indication a post was previously published, as wordpress remembers only the old versus new transitions.

I recommend you add some meta data, that is use add_action('publish_post', 'your_function') (or similar) to do update_post_meta($post->ID, 'post_published', 'on').

That way, every post gets an indicator when it's published - and you can then use the condition if (get_post_meta($post->ID, 'post_published', true)=='on') in your 'draft_to_publish' actions.

1
  • Yeah looks like there is no "previous status" I am sure I wont win many supporters but I can see value in this if just for trigger and plugin actions. The mediocre workaround I have had to implement is if(get_the_time('', $post->ID) == date("g:i a")) { however the issue here is if someone enters a publish date in the past, then this will get triggered. It's not a big deal because this can be handled on the collection end. – Dan Jul 29 '13 at 12:09
0

The hook publish_postwas also fired, if a published posts was updated. Use the hook transition_post_status for the hint, if the post was published. The hook have params. The action transition_post_status (as defined at http://codex.wordpress.org/Post_Status_Transitions) sends three parameters, $new_status, $old_status, $post.

add_action( 'transition_post_status', 'your_function', 10, 3 );
function your_function( $new_status, $old_status, $post ) {

   if ( 'draft' === $old_status && 'publish' ==== $new_status ) {
      // your todos
   }
}

Alternativly have WordPress a hook for the change from status to another status, also a example.

add_action( 'draft_to_publish', 'your_callback' );
function your_callback() {
    // Code here
}

See also this codex page to this topic and examples.

2
  • This would only work when immediately going from draft -> publish. The situation is this. - Post is Published (post gets a publish date as well) - Post is moved back to draft (Your above could trigger) - Post is later moved back to publish (only record of it being published is that the post has a publish date) I will need to use postmeta for this as suggested above. So when the post is moved back to draft, fire off a function that adds postmeta, then read this on publishing. Not ideal, I may need to look at the workflow my editors and publishers use to allow for review etc – Dan Jul 29 '13 at 23:59
  • The example works only on draft -> publish, but it is a example and you can change the check or the hook from the second solution. You can check for all changes on post status. – bueltge Aug 1 '13 at 11:45

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.