Update Post Meta of all published posts to show current date

258 views Asked by At

How can I Update Post Meta of all my published posts in WordPress to show current date and time each time any visitor click the post? Like a custom code in theme function to modify the the single post page to show visitor's current date and time at each visit

I tried using this code below but didn't work..

$time = current_time('mysql');

$post_type   = 'post'; // post, page, product or some other custom post type

// Get all the posts

$posts = get_posts( array( 

        'post_status' => 'published', 

        'post_type'   => $post_type 

  ));

foreach( $posts as $post ) {

  // We only want to update the post date

  $update = array(

        'ID'            => $post->ID,

        'post_date'     => $time,

        'post_date_gmt' => get_gmt_from_date( $time ),

  );

  //* Update the post

  wp_update_post( $update );
2

There are 2 answers

2
Asfand Yar Ali Khan On

The code you've provided is not updating the post meta instead, it's attempting to update the post date. Also, there are some small issues in your code snippet. Here's how you could achieve what you're looking for:

function update_post_meta_on_visit($post_id) {
if (is_single() && get_post_status($post_id) === 'publish') {
     $current_time = current_time('mysql');
     update_post_meta($post_id, 'last_visited', $current_time);
  }
}
    
add_action('wp', 'update_post_meta_on_visit');
17
Oleh Kosarenko On

I found a couple of issues in your code:

You need to replace: 'post_status' => 'published' to 'post_status' => 'publish'

Also, if you want to get all posts, you need to add 'posts_per_page' => -1

So, the code will be like this:

$posts = get_posts( array(
    'post_status' => 'publish',
    'post_type'   => 'post',
    'posts_per_page' => -1,
));

$current_time = current_time('mysql');
$current_time_gmt = get_gmt_from_date( $current_time );
foreach( $posts as $post ) {
    $update = array(
        'ID' => $post->ID,
        'post_date' => $current_time,
        'post_date_gmt' => get_gmt_from_date( $current_time_gmt ),
    );
    wp_update_post( $update );
}

If you want to use custom post fields to save the date, you can try this example:

$posts = get_posts( array(
    'post_status' => 'publish',
    'post_type'   => 'post',
    'posts_per_page' => -1,
));

$current_time = current_time('mysql');
foreach( $posts as $post ) {
    update_post_meta( $post->ID, 'custom_post_date', $current_time );
}

But if your site has many posts, these examples are not suitable because they result in multiple database requests.

I recommend using alternative methods. Here's an example for updating post data:

global $wpdb;
$current_time     = current_time( 'mysql' );
$current_time_gmt = get_gmt_from_date( $current_time );
$wpdb->query(
    $wpdb->prepare(
        "UPDATE $wpdb->posts SET post_date = %s, post_date_gmt = %s 
         WHERE post_type = 'post' AND post_status='publish'",
        $current_time,
        $current_time_gmt
    )
);

And here's an example for updating the custom post meta:

global $wpdb;
$current_time     = current_time( 'mysql' );
$wpdb->query(
    $wpdb->prepare(
        "UPDATE $wpdb->postmeta SET meta_value = %s 
        WHERE post_id IN (SELECT ID FROM $wpdb->posts WHERE post_type = 'post' AND post_status='publish') 
        AND meta_key = 'custom_post_date'",
        $current_time
    )
);