Changing the post date and time with function

What function can be used to update a post’s post date and time to the current date & time?

Related posts

Leave a Reply

2 comments

  1. Call wp_update_post() with a special value for 'post_date' and 'post_date_gmt':

    $time = current_time('mysql');
    
    wp_update_post(
        array (
            'ID'            => 123, // ID of the post to update
            'post_date'     => $time,
            'post_date_gmt' => get_gmt_from_date( $time )
        )
    );
    
  2. The selected answer is perfect for modifying the “publish date”, but in my case I wanted to change the “last modified” date (post_modified and post_modified_gmt in the db). The following is sufficient for that:

    wp_update_post( [ 'ID' => $id ] );