Update post date on every new comment?

From what i understand, you want to change the post’s modification time whenever a comment is left on your post. For this, you need to hook into the wp_insert_comment hook and update the post’s date manually:

add_action('wp_insert_comment','update_post_time',99,2);
function update_post_time($comment_id, $comment_object) {
    // Get the post's ID
    $post_id = $comment_object->comment_post_ID;
    // Double check for post's ID, since this value is mandatory in wp_update_post()
    if ($post_id) {
        // Get the current time
        $time = current_time('mysql');
        // Form an array of data to be updated
        $post_data = array(
            'ID'           => $post_id, 
            'post_modified'   => $time, 
            'post_modified_gmt' =>  get_gmt_from_date( $time )
        );
        // Update the post
        wp_update_post( $post_data );
    }
}

Note that this will create a revision for the post each time a comment is created.

If your sitemap plugin uses the post_date instead of post_modified, you can use this instead:

$post_data = array(
    'ID'           => $post_id, 
    'post_date'   => $time, 
    'post_date_gmt' =>  get_gmt_from_date( $time )
);

However, this might cause problems, and mess post’s order in archives and homepage, since it changes the post’s creation date, not modification date.

Leave a Comment