1 回答
TA贡献1813条经验 获得超2个赞
将解决方案发布到我自己的问题,以防万一它可以帮助任何遇到类似问题的人。
<?php
// Date is past, but not within the last 30 days
$meta_query_past = array(
array(
'key' => 'event_date',
'value' => date('Ymd', strtotime('-30 days')),
'type' => 'DATE',
'compare' => '<'
)
);
// Date is recent
$meta_query_recent = array(
'relation' => 'AND',
// Date is more than date 30 days ago
array(
'key' => 'event_date',
'value' => date('Ymd', strtotime('-30 days')),
'type' => 'DATE',
'compare' => '>='
),
// Date is less than todays date
array(
'key' => 'event_date',
'value' => date('Ymd'),
'type' => 'DATE',
'compare' => '<'
)
);
$meta_query_future = array(
'relation' => 'OR',
// Date is future
array(
'key' => 'event_date',
'value' => date('Ymd'),
'type' => 'DATE',
'compare' => '>='
),
// Date is empty
array(
'key' => 'event_date',
'value' => '',
'compare' => '='
)
);
if ( get_sub_field('show_events') == 'past' ) {
$meta_query = $meta_query_past;
$attending = "We attended this event";
$show_events = 'past';
$sort_order = 'DESC';
} elseif ( get_sub_field('show_events') == 'recent' ) {
$meta_query = $meta_query_recent;
$attending = "We recently attended this event";
$show_events = 'recent';
$sort_order = 'DESC';
} else {
$meta_query = $meta_query_future;
$attending = "We're attending this event";
$show_events = 'future';
$sort_order = 'ASC';
}
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'order' => $sort_order,
'orderby' => 'meta_value_num',
'meta_key' => 'event_date',
'meta_query' => $meta_query
);
?>
- 1 回答
- 0 关注
- 102 浏览
添加回答
举报