简体   繁体   中英

How can I order a wp_query with date and then time

I am using Advanced custom fields with separate date and time fields for events. What I need to do is show all the events that haven't yet occurred.

I have managed to show the events in order by date so far but what I need to do now is now have each one in order by the time they will occur also. Once the time has occurred the event needs to be removed from the list.

My args for the events list so far looks like this...

$today = date('Ymd');
$time = date('H:i:s');
$compCount = array(
  'post_type' => 'product',
  'posts_per_page'  => -1,
  'meta_query' => array(
    array(
      'key'     => 'comp_closing',
      'compare' => '>=',
      'value'       => $today,
    )
  ),
  'orderby'   => 'meta_value_num',
  'order'     => 'ASC',
);

my field for the date is comp_closing and the time is closing_time .

I have tried using relation with 2 different meta arrays but have found it confusing to get anything to work correctly.

Although I don't know how the fields comp_closing and closing_time are stored in the database this may point you in the right direction.

$today = date('Y-m-d');
$time = date('H:i:s');

$compCount = array(
  'post_type' => 'product',
  'posts_per_page'  => -1,
  'meta_query' => array(
    'relation' => 'OR',
    // make sure the date is after the current date...
    array(
      'key'     => 'comp_closing',
      'compare' => '>',
      'value'       => $today,
    ),
    // ...or if the date is the same...
    array(
      'relation' => 'AND',
      array(
        'key'     => 'comp_closing',
        'compare' => '=',
        'value'       => $today,
      ),
      // ...make sure we didn’t hit the time yet.
      array(
        'key'     => 'closing_time',
        'compare' => '>',
        'value'       => $time,
      )
    )
  ),
  'orderby'   => 'meta_value_num',
  'order'     => 'ASC',
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM