如何订购带有日期和时间的 wp_query

How can I order a wp_query with date and then time

我正在为事件使用具有单独日期和时间字段的高级自定义字段。我需要做的是显示所有尚未发生的事件。

到目前为止,我已经设法按日期顺序显示事件,但我现在需要做的是在事件发生的时间之前对每个事件进行排序。一旦时间到了,就需要从列表中删除该事件。

到目前为止,我的事件列表参数如下所示...

$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',
);

我的日期字段是 comp_closing,时间是 closing_time

我曾尝试将 relation 与 2 个不同的元数组一起使用,但发现让任何东西正常工作都很混乱。

虽然我不知道字段 comp_closingclosing_time 是如何存储在数据库中的,但这可能会为您指明正确的方向。

$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',
);