[Resolved] Set publish date of post

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to set the publish date and time of a post within the CRED Form used to create that post.

Solution: There's no post publish date field built into CRED, so this will require some custom code and a generic field. Add a generic date field to your CRED form, then add this code to functions.php:

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

- Change the form id 12345 to match your form.
- Change 'set-new-date' to match the slug of your generic date field

Relevant Documentation:
https://wp-types.com/documentation/user-guides/inserting-generic-fields-into-forms/
https://wp-types.com/documentation/programmer-reference/cred-api/#cred_save_data

Related documentation: Front-end Content Submission and Editing Forms

This topic contains 4 replies, has 2 voices, and was last updated by  chrisH-10 1 month, 4 weeks ago. The support staff assigned to this topic is Christian Cox.

Viewing 5 posts - 1 through 5 (of 5 total)
Author
Posts
#568857

Tell us what you are trying to do?
I am trying to allow a user to create a new post (defaults to Pending Review) or edit an existing one. I want them to set the date/time they would like to schedule this for. I'd preferably use the standard publish date so the scheduling is all taken care of.

Is there any documentation that you are following?
I could not use the field using the CRED UI. Could not find anything relevant in docs. Found some old, tangential examples in a ticket that required a custom field and a PHP hook.

#568992

Christian Cox
Forum moderator

Supporter languages: English (English )

Supporter Timezone: America/New_York (GMT-05:00)

Hi, there's no built-in publish date input in CRED, so you'll still be required to use a generic field and cred_save_data hook. I can help set that up if you'd like. I'm not quite clear on the Pending Review status and how it will be used. Is there an approval process on the back-end of your site? If so, how does that work with your delayed publishing time - what if the post is not approved before that time?

If there's no approval process, would a "future" post status work better?

#569815

No worries about the status, we've eliminated the need for the workflow at the moment.

An example for the cred_save_data hook would be appreciated and I would think applicable to others. I thought I had seen one here in the forums, but I couldn't find it again.

#570062

Christian Cox
Forum moderator

Supporter languages: English (English )

Supporter Timezone: America/New_York (GMT-05:00)

First, add a generic date field to your CRED form. Then you can use this code as a starting point:

add_action('cred_save_data', 'set_post_date_to', 100, 3);
function set_post_date_to($post_id, $form_data) {
  if( $form_data['id'] == 12345 ) {
    $newDate = $_POST['set-new-date']['datetime'] . ' 00:00:00';
    $my_post = array(
      'ID'           => $post_id,
      'post_date' => $newDate,
      'post_date_gmt' => $newDate,
      'post_status' => 'future'
    );

    // Update the post into the database
    wp_update_post( $my_post );
  }
}

- Change the form id 12345 to match your form.
- Change 'set-new-date' to match the slug of your generic date field

#572335

Thanks, that works perfectly.

I made the minor enhancement below to also capture the user entered hour and minute.

$newDate = $_POST['wpcf-push_date']['datetime'] . ' '.$_POST['wpcf-push_date']['hour'].':'.$_POST['wpcf-push_date']['minute'].':00';
Viewing 5 posts - 1 through 5 (of 5 total)