请注意orderby
数组需要引用中的数组meta_query
其中包含直接key
项目,例如。\'key\' => \'tdlrm_mp\'
. 例如,您应该使用\'tdlrm_mp_exists\' => \'ASC\',
而不是\'tdlrm_mp_key\' => \'ASC\'
在您的$args[\'orderby\']
.
然而,这实际上并不能按照您希望的方式对帖子进行排序,但我只是向您展示了应该使用的正确语法。
但不要担心,你想做的是可能的:)
下面是如何设置meta_query
收件人:
$args[\'meta_query\'] = array(
array(
\'relation\' => \'OR\',
// select posts that have the tdlrm_mp meta
\'has_tdlrm_mp\' => array(
\'key\' => \'tdlrm_mp\',
\'type\' => \'NUMERIC\',
),
// select posts that don\'t have the meta
\'no_tdlrm_mp\' => array(
\'key\' => \'tdlrm_mp\',
\'compare\' => \'NOT EXISTS\',
),
),
array(
\'relation\' => \'OR\',
// select posts that have the 1C_quantity_total meta
\'has_1C_quantity_total\' => array(
\'key\' => \'1C_quantity_total\',
\'type\' => \'NUMERIC\',
),
// select posts that don\'t have the meta
\'no_1C_quantity_total\' => array(
\'key\' => \'1C_quantity_total\',
\'compare\' => \'NOT EXISTS\',
),
),
);
设置
orderby
到
none
因为我们将使用完全定制的。
Before 你跑吧new WP_Query()
, 使用posts_orderby
filter 要修改ORDER BY
子句,基本上我们使用CASE
operator 要实现您想要的(相当复杂)排序,请执行以下操作:$_filter = true; // enable the filter below
add_filter( \'posts_orderby\', function ( $orderby, $query ) use ( &$_filter ) {
if ( $_filter ) {
global $wpdb;
$meta_clauses = $query->meta_query->get_clauses();
$has_tdlrm_mp = $meta_clauses[\'has_tdlrm_mp\'][\'alias\'];
$has_1C_quantity_total = $meta_clauses[\'has_1C_quantity_total\'][\'alias\'];
// 1st, sort by the meta key, and posts without the meta are placed at
// the bottom (or 3rd position).
$orderby = "
CASE {$has_tdlrm_mp}.meta_key
WHEN \'tdlrm_mp\' THEN 1
WHEN \'1C_quantity_total\' THEN 2
ELSE 3
END ASC";
// 2nd, sort by the meta value, only for the posts in position 1 and 2
// above.
$orderby .= ",
CASE {$has_tdlrm_mp}.meta_key
WHEN \'tdlrm_mp\' THEN {$has_tdlrm_mp}.meta_value+0
END ASC,
CASE {$has_1C_quantity_total}.meta_key
WHEN \'1C_quantity_total\' THEN {$has_1C_quantity_total}.meta_value+0
END DESC";
// 3rd, now optionally sort the posts in the 3rd position above. Here,
// we sort them by the post date.
$orderby .= ", {$wpdb->posts}.post_date DESC";
}
return $orderby;
}, 10, 2 );
$query = new WP_Query( $args );
$_filter = false; // disable the filter above
更新
抱歉,我修改了几次答案,但实际上我仍然忘记更正表别名(在上面的步骤3中)。
现在我已经更正了它,此外,我还使用了WP_Meta_Query::get_clauses()
而不是对(表)别名进行硬编码。非常感谢@Artem 还有他的other question!