How to update post_modified FAST after each comment?

One of my ways of keeping my posts up to date is to update the post_modified value after some of certain actions like commenting, liking…

You shouldn’t use wp_update_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.

This process will take way longer than using direct DB query.

function vl_post_modified_update ($post_id) {
global $wpdb;
// Get the current time
$time = current_time('mysql');
$post_modified_gmt = get_gmt_from_date($time);

$post_id = 123; //the post ID;

$res = $wpdb->query("UPDATE $wpdb->posts SET post_modified = '{$time}', post_modified_gmt = '{$post_modified_gmt}'  WHERE ID = {$post_id}" );
return $res;
}

Next step is to hook this function after user comment is submited

add_action('wp_insert_comment','vl_post_modified_update',99,1);

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>