To change Post published date each time it is viewed, we can use the the_post
action hook that gets fired every time a post is set up in the global $post
variable.
Here’s an example code snippet that you can add to your theme’s functions.php
file or a custom plugin:
function wpxss_update_post_timestamp_on_view() {
if (is_single()) { // only update for single post pages
$post_id = get_the_ID();
$current_time = current_time('mysql');
$post_data = array(
'ID' => $post_id,
'post_modified' => $current_time,
'post_modified_gmt' => get_gmt_from_date($current_time),
);
wp_update_post($post_data);
}
}
add_action('the_post', 'wpxss_update_post_timestamp_on_view');
Changing the post timestamp each time a post is viewed will affect the post’s ordering in the blog archive pages and will cause the recently viewed posts to always be shown on the top/front page.
Was this post helpful?
Let me know if you liked the post. That’s the only way I can improve. 🙂