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

问题描述 投票:1回答: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

我曾尝试将relation与2个不同的元数组一起使用,但发现使任何东西正常工作令人困惑。

php wordpress advanced-custom-fields meta-query
1个回答
0
投票

尽管我不知道字段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',
);
© www.soinside.com 2019 - 2024. All rights reserved.