I have used this code below so that in my frontend form to edit a post, when I change the date for publish-note-date field, it changes the post date of that post:
//sermon publish date
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==26 || 25)
{
$timestamp = get_post_meta($post_id, 'wpcf-publish-note-date', true);
$date = date('Y-m-d H:i:s', $timestamp);
if(isset($timestamp)){
$args = array(
'ID' => $post_id,
'post_date' => $date
);
wp_update_post( $args );
}
}
}
The problem is, even though it IS changing the post date correctly, it doesn't change the actual status of the post. Meaning if the post date is now but I edit it to sometime in the future, it should then become a SCHEDULED post. But it doesn't... it stays published UNTIL I hit the update button under quick edit, in the backend. Then it becomes 'scheduled'. How can this be fixed?
Hi Mike,
Thank you for contacting us and I'll be happy to assist.
For the post status to update to "future" ( scheduled ), you'll need to update the values of "post_date_gmt" and "edit_date" too.
( ref: https://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts )
//sermon publish date
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==26 || 25)
{
$timestamp = get_post_meta($post_id, 'wpcf-publish-note-date', true);
$date = date('Y-m-d H:i:s', $timestamp);
$postdate_gmt = gmdate('Y-m-d H:i:s',strtotime($date));
if(isset($timestamp)){
$args = array(
'ID' => $post_id,
'post_date_gmt' => $postdate_gmt,
'post_date' => $date,
'edit_date' => true,
);
wp_update_post( $args );
}
}
}
I hope this helps and for more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
My issue is resolved now. Thank you!