我想找到一种方法,先按姓氏排序,然后按名字排序。虽然姓氏+名字部分运气不太好,但我还是能说出姓氏。这就是我想到的:
$posts = get_posts(
[
\'post_type\' => \'soldier\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 5,
\'meta_key\' => \'last_name\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\',
\'meta_query\' => [
[
\'key\' => \'last_name\',
\'orderby\' => \'meta_value\',
\'order\' => ASC
],
[
\'key\' => \'first_name\',
\'orderby\' => \'meta_value\',
\'order\' => ASC
],
],
\'paged\' => $soldier_paged
]
);
最合适的回答,由SO网友:Milo 整理而成
您的代码有一些问题,有些是不相关的,但通常需要命名的元查询来按多个元键排序:
$posts = get_posts( [
\'post_type\' => \'soldier\',
\'posts_per_page\' => 5,
\'paged\' => $soldier_paged,
\'meta_query\' => [
\'last_name_clause\' => [
\'key\' => \'last_name\',
\'compare\' => \'EXISTS\'
],
\'first_name_clause\' => [
\'key\' => \'first_name\',
\'compare\' => \'EXISTS\'
],
],
\'orderby\' => [
\'last_name_clause\' => \'ASC\',
\'first_name_clause\' => \'ASC\',
],
] );