最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I list users by date in an array in meta_value?

programmeradmin4浏览0评论

I keep an order list of all users. I keep this list in the meta_key value named payments. The payments meta allows me to keep information about the payments the user made. To give an example for an user:

array(
    0 => array(
        'date' => '2020-04-15',
        'amount' => '750',
    ),
    1 => array(
        'date' => '2020-04-20',
        'amount' => '900',
    ),
    2 => array(
        'type' => 'installment',
        'installments' => array(
            0 => array(
                'date' => '2020-04-20',
                'amount' => '250',
            ),
            1 => array(
                'date' => '2020-05-20',
                'amount' => '250',
            ),
            2 => array(
                'date' => '2020-06-20',
                'amount' => '250',
            )
        )
    )
)

Based on this information, I try to list the users who have not paid. How can I list users with a payment older than today? I tried this but it didn't:

$today = date('Y-m-d');    
$wpdb->get_results("SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'payments' AND meta_value <= '%".$today."%'");

Alternative scenario:

Based on the comment made by @Himand, I produced an alternative scenario. I decided to keep each of a user's payments as a separate meta_value. For example:

For user 1:

I created a metadata that holds the total number of payments: _payments_count

And I gave a number to each payment in order. For example: _payment_detail_1 _payment_detail_2 _payment_detail_3.

Then I created a for loop depending on the number of payments.

$payment_count = get_user_meta( $user_id, '_payments_count', true );
for ($i = 1; $i <= $payment_count ; $i++) {
    $payment_detail = get_user_meta( $user_id, '_payment_detail_'.$i, true );
}

This way I can access all payment data, but I just don't know what to do when I want to list older payments than today. After I pull all the payment information, I can make it list the older ones today with an if statement. Like this:

$payment_count = get_user_meta( $user_id, '_payments_count', true );
for ($i = 1; $i <= $payment_count ; $i++) {
    $payment_detail = get_user_meta( $user_id, '_payment_detail_'.$i, true );
    if ( $payment_detail['payment_date'] < date('Y-m-d') ){
        // ......
    }
}

However, this means a big loss of performance. Because there are thousands of users registered in the system. I want to list the payments by comparing the date information directly.

Anyone have an idea about this?

发布评论

评论列表(0)

  1. 暂无评论