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 );
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: