How to change the modified time of a wordpress post?

We are able to make use of ajax to update our post_meta as we wanted. However, it does not change the modified_time of the post.

We depend on the get_modified_time to show users when the post was last updated. (The newer the better)

Read More

I have searched around, and I don’t see anyone using this technique yet.

Does anyone have an answer?

Thanks!

Related posts

Leave a Reply

4 comments

  1. I used wpdb::query() to do this:

    global $wpdb;
    
    //eg. time one year ago..
    $time = time() - DAY_IN_SECONDS * 365;
    
    $mysql_time_format= "Y-m-d H:i:s";
    
    $post_modified = gmdate( $mysql_time_format, $time );
    
    $post_modified_gmt = gmdate( $mysql_time_format, ( $time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS )  );
    
    $post_id = /*the post id*/;
    
    $wpdb->query("UPDATE $wpdb->posts SET post_modified = '{$post_modified}', post_modified_gmt = '{$post_modified_gmt}'  WHERE ID = {$post_id}" );
    

    Note: You can’t use wp_update_post() if you want to explicity set the modified date(s) on the post, because it calls wp_insert_post(), which determines that the post exists and sets the post_modified and post_modified variables to the current date.

  2. Very simple in PHP, where 80 is the post number:

    // update post_modified and post_modified_gmt `datetime` on a post
    $update = array( 'ID' => 80 );
    wp_update_post( $update );