Question

I let users add posts in draft status. After I check and see that everything is ok, I publish them. I noticed that drafts created two days ago change date after being published. Is there any way of keeping that original date?

Was it helpful?

Solution

you need to manually set the publish date.

OTHER TIPS

The code below probably needs some adjusting, but the trick here is to obtain the date of the first revision and apply it to the post being published:

add_action(  'transition_post_status',  function ( $new_status, $old_status, $post )
{
  if ( $old_status == 'pending'  &&  $new_status == 'publish' 
       && $post->post_date_gmt == $post->post_modified_gmt) {
    $revisions = wp_get_post_revisions($post->ID);
    $oldest = NULL;
    foreach($revisions as $revision){
      $oldest = $revision->ID;
    };
    $previousdate = get_the_date( 'Y-m-d H:i:s', $oldest );
    wp_update_post(
      array (
        'ID'            => $post->ID,
        'post_date'     => $previousdate,
        'post_date_gmt' => get_gmt_from_date( $previousdate )
      )
    );
  }

}, 10, 3 );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top