0

In my search.php file i have a loop which succesfuly gives list of search query.


    global $wp_query;
    $total_results = $wp_query->posts; 

    if ( $total_results ) : 
        foreach ( $total_results as $tr ) : 

        //(...)

        endforeach;
    endif;

But there is something wrong with date time in results because the_time() function displays a wrong date for all listed posts. Here is what i get:

the_time( 'j F Y, G:i' ); // --> 7 NOVEMBER 2023, 20:00 (don't know what kind a date is that)

echo get_the_date( 'j F Y, G:i' ); --> 7 NOVEMBER 2023, 20:00

echo $tr->post_date; // --> 2023-10-13 23:00:47 (proper publish date)

Is my query is right? Apart from date, meta data of all listed posts are correct (title, author etc.).

1 Answer 1

1

All the date functions reference the global $post variable, but from your code the $post variable is not being used, so that's why the date is wrong.

Try this (untested):

foreach ( $total_results as $tr ) {
    echo get_the_time( 'j F Y, G:i', $tr );
}
1
  • Indeed. I was also thinking that lack of global $post instance could be the reason but I figured it out later. I made a change to <?php echo get_the_date( 'j F Y, G:i', $tr ); ?> and it works fine. Same as Your proposition with get_the_time(). Thanks.
    – X9DESIGN
    Commented Nov 23, 2023 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.