繁体   English   中英

如何订购带有日期和时间的 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

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

尽管我不知道字段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',
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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