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.
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.
Share Improve this question asked Mar 23, 2019 at 18:33 Stephen GStephen G 114 bronze badges 1- Have you tried $my_post['post_status'] = 'future'; just like you're setting the post_date? – tmdesigned Commented Mar 23, 2019 at 19:35
1 Answer
Reset to default 1wp_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.