admin管理员组

文章数量:1129647

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.).

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.).

Share Improve this question edited Nov 22, 2023 at 19:27 X9DESIGN asked Nov 22, 2023 at 19:22 X9DESIGNX9DESIGN 1699 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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 );
}

本文标签: loopDifferent date time in results of search