我和orderby有点小问题。我正在寻找orderby ASC(lastname+firstname)的解决方案示例:
Selanne Andy,Selanne Beeny,Selanne Teemu,但现在它是这样排列的:
Selanne Beeny这里是代码:
$order_by_1 = array(
\'meta_key\' => \'last_name\',
\'meta_query\' => array(
array(
\'key\' => \'last_name\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\'
),
array(
\'key\' => \'first_name\',
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\'
),
),
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\'
);
$usr_1 = get_users($order_by_1);
// Array of stdClass objects.
foreach ( $usr_1 as $user ) { ....
谢谢你的帮助。
最合适的回答,由SO网友:Tom J Nowell 整理而成
要做到这一点,您需要根据的官方开发人员文档使用命名元查询子句WP_Query
:
带有多个“meta\\u key”的“orderby”如果您希望按两个不同的postmeta排序(例如,城市第一和州第二),则需要使用“命名元查询”将元查询组合并链接到您的orderby数组。请参见以下示例:
\'meta_query\' => array(
\'relation\' => \'AND\',
\'state_clause\' => array(
\'key\' => \'state\',
\'value\' => \'Wisconsin\',
),
\'city_clause\' => array(
\'key\' => \'city\',
\'compare\' => \'EXISTS\',
),
),
\'orderby\' => array(
\'city_clause\' => \'ASC\',
\'state_clause\' => \'DESC\',
), ) ); ```
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
这个问题也在这里被问到并得到了回答:
Order by multiple meta key and meta value