如何按meta_value数组中的日期对用户进行排序

时间:2020-04-21 作者:Faruk

我为用户提供了一个“payments”meta\\u密钥。我将从用户处收到的付款信息(日期和付款金额)存储在连接到此meta\\u键的meta\\u值中。例如:

array(
  [0] => array(
         \'date\' => \'2020-04-15\',
         \'amount\' => \'750\'
       ),
  [1] => array(
         \'date\' => \'2020-04-20\',
         \'amount\' => \'900\'
       )
)
我想在此meta\\u值中按上次付款的日期对用户进行排序。示例:

用户1付款:

array(
  [0] => array(
         \'date\' => \'2020-04-15\',
         \'amount\' => \'750\'
       ),
  [1] => array(
         \'date\' => \'2020-04-20\',
         \'amount\' => \'900\'
       )
)
用户2付款:

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\'
       )
)
用户3付款:

array(
  [0] => array(
         \'date\' => \'2020-04-24\',
         \'amount\' => \'750\'
       )
)
用户列表:

1-用户2
2-用户3
3-用户1

是否可以使用get\\u users函数执行此操作?我现在用这个来列出用户:

$args = array(
    \'meta_query\' => array(
        array(
            \'key\' => \'payments\',
            \'value\' => \'\',
            \'compare\' => \'!=\'
        )
    ),
    \'orderby\' => \'display_name\',
);
$users = get_users($args);
Note: 用户最近的付款总是添加到数组的末尾。

Sorry: 对不起,我的英语不好。

1 个回复
最合适的回答,由SO网友:jhned 整理而成

不,使用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.