Skip Navigation

[Resolved] Post date not updating post

This support ticket is created 5 years, 8 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by mikeH-3 5 years, 8 months ago.

Assisted by: Waqar.

Author
Posts
#1238000

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?

#1238151

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

#1238349

My issue is resolved now. Thank you!