我有两个活动帖子的自定义字段:event-start
和event-end
. 当我查询当前事件时,我想显示event-start
或者event-end
大于或等于今天的日期,我想按event-start
(不是默认的post\\u日期)。
我使用以下参数创建了一个查询:
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => $number,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'event-start\',
\'value\' => $today_is,
\'type\' => \'NUMERIC\',
\'compare\' => \'>=\'
),
array(
\'key\' => \'event-end\',
\'value\' => $today_is,
\'type\' => \'NUMERIC\',
\'compare\' => \'>=\'
)
),
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
);
查询得到了正确的帖子,但问题是我无法让orderby正常工作。这组参数运行以下查询:
SELECT SQL_CALC_FOUND_ROWS wp_18_posts.ID FROM wp_18_posts INNER JOIN wp_18_postmeta ON (wp_18_posts.ID = wp_18_postmeta.post_id)
INNER JOIN wp_18_postmeta AS mt1 ON (wp_18_posts.ID = mt1.post_id) WHERE 1=1 AND wp_18_posts.post_type = \'post\' AND (wp_18_posts.post_status = \'publish\' OR wp_18_posts.post_status = \'private\') AND ( (wp_18_postmeta.meta_key = \'event-start\' AND CAST(wp_18_postmeta.meta_value AS SIGNED) >= \'1344289896\')
OR (mt1.meta_key = \'event-end\' AND CAST(mt1.meta_value AS SIGNED) >= \'1344289896\') ) GROUP BY wp_18_posts.ID **ORDER BY wp_18_posts.post_date** ASC LIMIT 0, 3
请注意,ORDER BY子句仍设置为post\\U date。如果我将查询参数更改为使用旧样式的meta\\u键以及orderby meta\\u值以及新样式的meta\\u查询,如下所示:
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => $number,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'event-start\',
\'value\' => $today_is,
\'type\' => \'NUMERIC\',
\'compare\' => \'>=\'
),
array(
\'key\' => \'event-end\',
\'value\' => $today_is,
\'type\' => \'NUMERIC\',
\'compare\' => \'>=\'
)
),
\'orderby\' => \'meta_value\',
\'meta_key\' => \'event-start\',
\'order\' => \'ASC\',
);
我最终得到以下查询:
SELECT SQL_CALC_FOUND_ROWS wp_18_posts.ID FROM wp_18_posts INNER JOIN wp_18_postmeta ON (wp_18_posts.ID = wp_18_postmeta.post_id)
INNER JOIN wp_18_postmeta AS mt1 ON (wp_18_posts.ID = mt1.post_id)
INNER JOIN wp_18_postmeta AS mt2 ON (wp_18_posts.ID = mt2.post_id) WHERE 1=1 AND wp_18_posts.post_type = \'post\' AND (wp_18_posts.post_status = \'publish\' OR wp_18_posts.post_status = \'private\') AND (wp_18_postmeta.meta_key = \'event-start\'
OR (mt1.meta_key = \'event-start\' AND CAST(mt1.meta_value AS SIGNED) >= \'1344289518\')
OR (mt2.meta_key = \'event-end\' AND CAST(mt2.meta_value AS SIGNED) >= \'1344289518\') ) GROUP BY wp_18_posts.ID ORDER BY wp_18_postmeta.meta_value ASC LIMIT 0, 3
用一个
event-start
元键,而不考虑为该键设置的日期。
如何通过event-start
如有价值,将不胜感激。
最合适的回答,由SO网友:DaveE 整理而成
以下是我找到的最佳解决方案:
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => $number,
\'meta_key\' => \'event-start\',
\'meta_value\' => $today_is,
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
\'key\' => \'event-start\',
\'value\' => $today_is,
\'type\' => \'NUMERIC\',
\'compare\' => \'>=\',
),
array(
\'key\' => \'event-end\',
\'value\' => $today_is,
\'type\' => \'NUMERIC\',
\'compare\' => \'>=\',
)
),
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
);
它使用相同的复合体
meta_query
, 但我添加了一个单独的
meta_key
和
meta_value
为了触发
meta_value
订货人。
如果我没记错的话,在我写这篇文章的时候,我曾尝试过这个解决方案,但没有成功。我还相信,我在WordPress Core中遇到了一张与此问题相关的Trac票证,但我一生都找不到它。
一句话:在最初发布这篇文章时,我认为有一个WordPress bug阻止了它的工作,但现在它可以工作了。