0

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 Answers2

0

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');
0

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
    )
);
Oleh Kosarenko
  • 436
  • 2
  • 7
  • Thanks so much, The last code works but it only shows current date but no time... How do I display the current time beside it? 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 ) ); – 247NAIJABUZZ ENTERTAINMENT Aug 12 '23 at 22:56
  • You're welcome. I believe the problem is that your template does not specify the format for displaying the date. You need to locate `the the_date()` or `get_the_date()` functions and specify the desired format within them. For instance, like this: `the_date( 'Y-m-d H:i:s', '

    ', '

    ' );` or `echo get_the_date('Y-m-d H:i:s');`
    – Oleh Kosarenko Aug 12 '23 at 23:15
  • Okay thanks so much again, Do I have to alter the original theme files for this or I can have a custom code to display this? I'm thinking if I alter the original theme function, Updating the theme will wipe the whole thing ... Or what do you suggest? – 247NAIJABUZZ ENTERTAINMENT Aug 13 '23 at 00:03
  • You're welcome. Everything depends on the situation. If you're concerned about losing your modifications after an update, you can create a [child theme](https://developer.wordpress.org/themes/advanced-topics/child-themes/) and make any changes there. – Oleh Kosarenko Aug 13 '23 at 00:24
  • I'm using child theme with the initial code.... Can you give me the code to add to the child theme again for the time to display instead of altering the original theme file? – 247NAIJABUZZ ENTERTAINMENT Aug 13 '23 at 00:27
  • Sure. You can use this code in the loop: ``. – Oleh Kosarenko Aug 13 '23 at 00:32
  • Please I'm not versatile with codings like that... What do you mean by code in the loop? – 247NAIJABUZZ ENTERTAINMENT Aug 13 '23 at 00:35
  • Additionally, you can create a new question and attach the code from any file in your theme. Then simply provide the link to the new question here, and I will help you make the necessary changes. – Oleh Kosarenko Aug 13 '23 at 00:35
  • Regarding the loop, you can read about it [here](https://codex.wordpress.org/The_Loop). – Oleh Kosarenko Aug 13 '23 at 00:37
  • Thanks... I altered the original theme template tag that has echo get the date by adding "Y-m-d H-i" but the time is static 23:23 on all posts... It doesn't change after reloading the page after some minutes – 247NAIJABUZZ ENTERTAINMENT Aug 13 '23 at 01:22
  • Sorry! It seems I completely forgot that you used the custom fields for updating the date. In this case, you need to use this code to display the date and time: `` – Oleh Kosarenko Aug 13 '23 at 01:38
  • But can't we merge the previous working custom code with this new code to make it a single code to put in theme function instead? – 247NAIJABUZZ ENTERTAINMENT Aug 13 '23 at 01:42
  • Their is another issue the date fails to update according to the current date... Which means all the post were only updated to 12th August but never gets updated thereafter according to the current date and time of the visitors... – 247NAIJABUZZ ENTERTAINMENT Aug 13 '23 at 02:08
  • I would not recommend merging the code. If you're looking for a simpler solution to insert into a template, that code can be wrapped in a custom function and then utilized. As for the posts being updated only until August 12, I suspect that you might have a caching plugin active on your site. I tested the functions on my local machine, and everything worked correctly for me. – Oleh Kosarenko Aug 13 '23 at 10:53
  • In the city where I live, it is now `16:57` and I see the date on your site `Sunday, 13 August 2023, 1:57 pm`. Therefore, I'm sure it's server time. You can also write arbitrary times to the database to make sure it works. For example: `$time = '2020-01-01 14:04:14';` – Oleh Kosarenko Aug 13 '23 at 14:05
  • Honestly, I don't understand how that works... Does that mean they are not even using any code to achieve that? – 247NAIJABUZZ ENTERTAINMENT Aug 13 '23 at 14:12
  • Maybe they just display the server time. You don't need to write to the database to do this. For example: `` – Oleh Kosarenko Aug 13 '23 at 14:16
  • You're welcome!)) – Oleh Kosarenko Aug 13 '23 at 15:25