1

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

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

到目前为止,我的事件列表参数看起来像这样......

$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

我尝试过使用relation2 个不同的元数组,但发现让任何东西正常工作都令人困惑。

4

1 回答 1

1

虽然我不知道这些字段是如何存储在数据库中的,但这可能会为您指明正确的方向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',
);
于 2020-03-09T13:06:10.750 回答