How to change the modified time of a wordpress post?

wordpress

Solution

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.

Problem

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) I have searched around, and I don't see anyone using this technique yet. Does anyone have an answer? Thanks!

Original source