Help!!! Post Meta to Display Current Date & Time Instead of Published Date (Custom Theme Function)

Oluwaxheun

Newbie
Joined
Mar 31, 2021
Messages
22
Reaction score
2
Please, Who can help me with the theme function code that can display current date and time each time a visitor clicks, on all my single posts in WordPress instead of the published date?



I tried the 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 );
 

Attachments

  • Screenshot_2023-08-11-18-15-49-184_com.android.chrome-edit.jpg
    Screenshot_2023-08-11-18-15-49-184_com.android.chrome-edit.jpg
    46.3 KB · Views: 1
Back
Top