0
我需要构建一个WP_Query,它可以通过某些高级自定义字段来过滤帖子。以下要点描述了所需的功能:ACF计划过滤器
- 今天比start_date大且小于'end_date'。
- 如果今天是'start_date',则测试时间是否大于'start_time'。
- 如果今天是'end_date',那么文本的时间小于'end_time'。
- 只检索第一篇文章,按自定义字段'priority'排序。
我奋力打造典型的WHERE clausule,如:
SELECT * FROM x WHERE (
('start_date' < today AND 'end_date' > today)
OR
('start_date' = today AND 'start_hour' < now)
OR
('end_date' = today AND 'end_hour' > now)
)
ORDER BY y
LIMIT 0,1;
我已经有一段时间考虑这个问题,我认为这将有可能通过嵌套在WP_Query ARGS meta_queries ...没有工作(参见下面的参数)。
$now_date = date('Ymd');
$now_time = date('Hi');
$args = array(
'post_type' => 'post',
'cat' => 4, /* Bulletin */
'posts_per_page' => 1,
'post_parent' => 0,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'bu_from',
'compare' => '<',
'value' => $now_date,
'type' => 'NUMERIC'
),
array(
'key' => 'bu_to',
'compare' => '>',
'value' => $now_date,
'type' => 'NUMERIC'
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'bu_from',
'compare' => '=',
'value' => $now_date,
'type' => 'NUMERIC'
),
array(
'key' => 'bu_from_time',
'compare' => '<=',
'value' => $now_time,
'type' => 'NUMERIC'
)
),
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'bu_to',
'compare' => '=',
'value' => $now_date,
'type' => 'NUMERIC'
),
array(
'key' => 'bu_to_time',
'compare' => '>=',
'value' => $now_time,
'type' => 'NUMERIC'
)
)
),
'meta_key' => 'bu_priority',
'orderby' => 'meta_value',
'order' => 'ASC'
);
请求忽略所有的meta_query:
/* Taken from var_dump($the_query); */
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE 1=1
AND wp_posts.post_parent = 0
AND (wp_term_relationships.term_taxonomy_id IN (4))
AND wp_posts.post_type = 'post'
AND ((wp_posts.post_status = 'publish'))
AND (wp_postmeta.meta_key = 'bu_priority')
GROUP BY wp_posts.ID
ORDER BY wp_postmeta.meta_value ASC
LIMIT 0, 1
;