不,使用get_users
按照您概述的方式运行,因为get_users
不接受meta_query
以同样的方式论证get_posts
做
我认为有两种方法可以获得您想要的结果:要么1)获取用户,然后对其进行排序,要么2)使用$wpdb
编写直接数据库调用。如果您选择了该路径,那么您应该确保使用the transients API.
选项1路线可以是这样的:
<?php
// Assume that this is the data that we get back from get_users or a similar function
$users = array(
0 => array(
\'name\' => \'Tom\',
\'payments\' => array(
0 => array(
\'date\' => \'2020-04-15\',
\'amount\' => \'750\',
),
1 => array(
\'date\' => \'2020-04-20\',
\'amount\' => \'900\',
),
),
),
1 => array(
\'name\' => \'Dick\',
\'payments\' => array(
0 => array(
\'date\' => \'2020-04-10\',
\'amount\' => \'750\',
),
1 => array(
\'date\' => \'2020-04-15\',
\'amount\' => \'900\',
),
2 => array(
\'date\' => \'2020-04-25\',
\'amount\' => \'1250\',
),
),
),
2 => array(
\'name\' => \'Harry\',
\'payments\' => array(
0 => array(
\'date\' => \'2020-04-24\',
\'amount\' => \'750\',
),
),
),
);
function sort_users_by_date( $user1, $user2 ) {
$date1 = end($user1[\'payments\'])[\'date\'];
$date2 = end($user2[\'payments\'])[\'date\'];
return strtotime($date1) - strtotime($date2);
}
usort($users, \'sort_users_by_date\');
var_dump($users);
Demo
使用
$wpdb
是骗子。应该遵循以下原则:
global $wpdb;
$users = $wpdb->get_results( \'SELECT * FROM {$wpdb->prefix}users INNER JOIN {$wpdb->prefix}usermeta ON {$wpdb->prefix}users.ID = {$wpdb->prefix}usermeta.user_id WHERE {$wpdb->prefix}usermeta.meta_key = "last_payment_date" ORDER BY {$wpdb->prefix}usermeta.meta_value DESC\' );
本例假设了两件重要的事情:
您需要添加另一个名为“last\\u payment\\u date”的用户元密钥将其他付款添加到“用户付款”元项目时更新此值。这样,您就可以按单个值进行查询和排序,而无需在SQL语句中解析数组您可能应该使用$wpdb->prepare
还有这个。你可以找到documentation on that here.