Skip Navigation

[Resolved] How to edit post publish date in front end form

This support ticket is created 2 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by kirillS-3 2 years, 11 months ago.

Author
Posts
#2236091

I want front end form to add new post with pending for review status and future publish date.
I use this solution: https://toolset.com/forums/topic/set-publish-date-of-post/
Pending status works great but publish date doesn't work neither with custom date field neither with standard date field.

#2236093

to be clear here is my current code:


add_action('cred_save_data', 'set_post_date_to', 100, 3);
function set_post_date_to($post_id, $form_data) {
  if( $form_data['id'] == 1518083 ) {
    //$newDate = $_POST['set-new-date']['datetime'] . ' 00:00:00';
	$newDate = $_POST['wpcf-post-publishdate']['datetime'] . ' '.$_POST['wpcf-post-publishdate']['hour'].':'.$_POST['wpcf-post-publishdate']['minute'].':00';
    $my_post = array(
      'ID'           => $post_id,
      'post_date' => $newDate,
      'post_date_gmt' => $newDate,
      'post_status' => 'pending'
    );
 
    // Update the post into the database
    wp_update_post( $my_post );
  }
}

#2236277

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

To update the date it seems that you also need to pass an argument 'edit_date' => true for the date to be changed.

See https://developer.wordpress.org/reference/functions/wp_update_post/#comment-4418

#2236847

It works perfectly! Thank a lot!