1

I have a custom post type setup for events. I set the time/date for the event using Advance Custom Fields. I would like to set the publish date from the ACF value as well. I am very close to having this working properly.. My site saves the publish date on save, but it sets the post status to 'published' instead of 'future'.

Here is the code I am using in my functions.php to set the post_date:

function my_acf_save_post( $post_id ) {
    $acfDate = get_field('alert_time', $post_id);
    $my_post = array();
    $my_post['ID'] = $post_id;
    $my_post['post_date'] = $acfDate;
    wp_update_post( $my_post );

}

My front end is setup to display both published and future dates. I have an opt-in notification system that sends out an email when an event moves from future to published. When I initially save the event, it sets itself to published, which sends out an unwanted email. If there is any way to make that function above force the status to 'future', it would be a life saver. I have tried a bunch of different ideas but couldn't get any of them to work.

  • Have you tried $my_post['post_status'] = 'future'; just like you're setting the post_date? – tmdesigned Mar 23 at 19:35
0

wp_insert_post_data filters post data just before it is inserted into the database. Documentation

add_filter('wp_insert_post_data', 'schedule_post_on_publish', '99');

function schedule_post_on_publish($data) {

      $post_type = "event"; // Replace event with exact slug of your custom post type

      if ( $data['post_type'] == $post_type AND $data['post_status'] == 'publish' ) {

           $data['post_status'] = 'future';
       }

       return $data;
}

This may help.

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.