Home > Blockchain >  How to query orderby meta_value from current_time('mysql')?
How to query orderby meta_value from current_time('mysql')?

Time:10-14

i have custom meta with value from current_time('mysql').

the query i am using right now:

$args = [
         'post_type'        => 'komik',
         'post_status'      => 'publish',
         'order'            => 'DESC',
         'orderby'          => 'meta_value',
         'meta_key'         => 'komik_last_update',
         'no_found_rows'    => true,
         'posts_per_page'   => 20
     ];

but i am not getting the posts properly. for example the latest update is 2021-10-12 17:16:04 but from the query i get 2021-10-08 07:13:30. I am wondering is something wrong with my query?

info: i also use meta_value_num but still same result.

CodePudding user response:

You could try using the meta_query and evaluate the value as a date.

$args = array(
    'post_type'        => 'komik',
    'post_status'      => 'publish',
    'order'            => 'DESC',
    'orderby'          => 'meta_value',
    'meta_query'        => array(
        array(
            'key'     => 'komik_last_update',
            'compare' => 'EXISTS',
            'type'    => 'DATE',
        ),
    ),
    'no_found_rows'    => true,
    'posts_per_page'   => 20
);
  • Related