wp-content » plugins » Update WordPress Post Publish Date each time it is viewed!

Update WordPress Post Publish Date each time it is viewed!

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?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended