[英]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
.我的日期字段是comp_closing
,时间是closing_time
。
I have tried using relation
with 2 different meta arrays but have found it confusing to get anything to work correctly.我曾尝试使用与 2 个不同元数组的relation
,但发现让任何东西正常工作都令人困惑。
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.尽管我不知道字段comp_closing
和closing_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.