Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the modified time of a wordpress post?

Tags:

wordpress

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!

like image 903
Emerson F Avatar asked Dec 02 '22 19:12

Emerson F


2 Answers

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.

like image 110
Soft Bullets Avatar answered Dec 18 '22 09:12

Soft Bullets


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 );
like image 23
ow3n Avatar answered Dec 18 '22 08:12

ow3n