我正在尝试查询两种自定义帖子类型,exhibitions
和events
。
展览有exhibition_start_date
和exhibition_end_date
个自定义字段。
事件有event_date
个自定义字段。
我如何在一个orderby子句中对多个列进行排序?
我一直在考虑这个以供参考https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/。
理想情况下,我的结果如下:
我的查询到目前为止,没有正确排序:
$today = date('Ymd');
$past_date = date( 'Ymd', strtotime( $today . ' - 12 months' ) );
$args = array(
'post_type' => array( 'exhibition', 'event' ),
'meta_query' => array(
'relation' => 'OR',
'exhibition_clause' => array(
array (
'key' => 'exhibition_end_date',
'compare' => '<',
'value' => $today,
),
array (
'key' => 'exhibition_end_date',
'compare' => '>=',
'value' => $past_date,
)
),
'event_clause' => array(
array (
'key' => 'event_date',
'compare' => '<',
'value' => $today,
),
array (
'key' => 'event_date',
'compare' => '>=',
'value' => $past_date,
)
)
),
'orderby' => array(
'exhibition_clause' => 'DESC',
'event_clause' => 'DESC',
),
);
编辑:连接到&#39; posts_orderby&#39;过滤器:
过滤器:
function my_exhibition_event_date_posts_orderby( $orderby ) {
if ( is_exhibition_event_query() ) {
unset( $GLOBALS['is_exhibition_event_query'] );
return "IFNULL(mt1.meta_value+0, mt2.meta_value+0) DESC";
}
return $orderby;
}
add_filter( 'posts_orderby', 'my_exhibition_event_date_posts_orderby', 10, 1 );
sql输出:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) INNER JOIN wp_postmeta AS mt2 ON ( wp_posts.ID = mt2.post_id ) INNER JOIN wp_postmeta AS mt3 ON ( wp_posts.ID = mt3.post_id ) WHERE 1=1 AND (
(
( wp_postmeta.meta_key = 'exhibition_end_date' AND wp_postmeta.meta_value < '20161023' )
AND
( mt1.meta_key = 'exhibition_end_date' AND mt1.meta_value >= '20080623' )
)
OR
(
( mt2.meta_key = 'event_date' AND mt2.meta_value < '20161023' )
AND
( mt3.meta_key = 'event_date' AND mt3.meta_value >= '20080623' )
)
) AND wp_posts.post_type IN ('exhibition', 'event') AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY IFNULL(mt2.meta_value+0, mt1.meta_value+0) DESC LIMIT 0, 20
但是,这仍然是不对的。这是回复帖子的顺序:
何时应按结束日期排序,最先安排最新日期。
答案 0 :(得分:0)
一种选择是为查询orderby
设置一个大致含有以下内容的钩子:
add_filter( 'posts_orderby', 'my_posts_orderby', 10, 1 );
function my_posts_orderby( $orderby ) {
if (is_exhibition_event_query()) {
return 'IFNULL(exhibition_end_date, event_date) ASC';
}
return $orderby;
}
您需要实现is_exhibition_event_query()
以限制对此特定查询的此查询修改。一种选择是在查询之前设置全局变量,并在查询完成后取消设置它。
<强>更新强>
结束查询应如下所示:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE
( wp_postmeta.meta_key = 'exhibition_end_date' OR
wp_postmeta.meta_key = 'event_date' )
AND wp_postmeta.meta_value < '20161023'
AND wp_postmeta.meta_value >= '20080623'
AND wp_posts.post_type IN ('exhibition', 'event')
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private')
ORDER BY wp_postmeta.meta_value DESC
LIMIT 0, 20
我不确定,这是否可以通过WP_Query
实现。对于非平凡的查询,我总是使用直接SQL查询(wpdb->get_results()
)。除posts_orderby
之外,查询的其他部分也有类似的钩子,因此有时使用WP_Query
并使用一个或多个钩子调整查询更为直接。
答案 1 :(得分:0)
不幸的是,我无法找到正确的查询来直接从数据库中获取我想要的订单。但是,我确实提出了一个简单的usort
函数来比较元值。
为了进一步复杂化订购所需的SQL,exhibition_start_date
以Ymd
格式存储,而event_date
以格式Y-m-d H:i:s
存储。
usort( $the_query->posts, 'my_compare_exhibition_and_event_dates_descending' );
function my_compare_exhibition_and_event_dates_descending($a, $b) {
$a_date = get_post_meta( $a->ID, 'exhibition_start_date', true );
if ( empty( $a_date ) ) {
$a_date = get_post_meta( $a->ID, 'event_date', true );
$a_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $a_date )->format( 'Ymd' );
}
$b_date = get_post_meta( $b->ID, 'exhibition_start_date', true );
if ( empty( $b_date ) ) {
$b_date = get_post_meta( $b->ID, 'event_date', true );
$b_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $b_date )->format( 'Ymd' );
}
return ( $a_date > $b_date ) ? -1 : 1;
}