0

I want to list all users who registered in between the $start_date and $end_date.I have written a metaquery,but it returns empty.Please help.

$start_date        =  2014-08-16 09:28:17
$end_date          =  2015-08-16 09:28:17
$current_user_id   =  get_current_user_id();
$args = array(
'meta_query' => array(
    'relation' => 'AND',
        array(
            'key' => 'user_registered',
            'value' => $start_date,
            'compare' => '>',
            'type' => 'NUMERIC'
        ),
        array(
            'key' => 'user_registered',
            'value' => $end_date,
            'compare' => '<=',
            'type' => 'NUMERIC'
        ),
        array(
            'key'  => 'referral_id',
            'value' => 'current_user_id',
            'compare'=> '=',
            'type' => 'NUMERIC'
        )
    ),

);
$users = get_users($args);
1

EDIT

start and end date is following the date format. But you are using NUMERIC type casting in meta query. You need to use DATE type like

array(
            'key' => 'user_registered',
            'value' => $start_date,
            'compare' => '>',
            'type' => 'DATE'
        ),
        array(
            'key' => 'user_registered',
            'value' => $end_date,
            'compare' => '<=',
            'type' => 'DATE'
        )

Hope that this will help you.

Ahh...there have another problem. You are saving the data in $current_user_id but you are not using this variable in third array. Try this once

array(
            'key'  => 'referral_id',
            'value' => $current_user_id,
            'compare'=> '=',
            'type' => 'NUMERIC'
        )

Full Code:

$args = array(
   'meta_query' => array(
                         array(
                                'relation' => 'AND',
                                array(
                                    'key' => 'user_registered',
                                    'value' => $start_date,
                                    'compare' => '>',
                                    'type' => 'DATE'
                                ),
                                array(
                                    'key' => 'user_registered',
                                    'value' => $end_date,
                                    'compare' => '<=',
                                    'type' => 'DATE'
                                ),
                                array(
                                    'key'  => 'referral_id',
                                    'value' => $current_user_id,
                                    'compare'=> '=',
                                    'type' => 'NUMERIC'
                                )
                        )
                  )
);
$users = get_users($args);

Try my full code now.

  • NO still it returns an empty array. :( – John Aug 21 '14 at 10:56
  • Ok. I am editing my answer and giving the full code – Chinmoy Kumar Paul Aug 21 '14 at 11:08
  • Bro.It working but it returns all the users not getting filtered. – John Aug 21 '14 at 11:22
  • Ok. this is great news change the variables like this $start_date = '2014-08-16 09:28:17'; $end_date = '2015-08-16 09:28:17'; – Chinmoy Kumar Paul Aug 21 '14 at 11:26
  • 1
    The field user_registered is from table wp_users and referral_id is from wp_usermeta.Does it matter ? – John Aug 21 '14 at 11:37
1

The correct approach is now to use WP_Date_Query. See the date_query field in get_users() is described as:

An array that is handled through to the WP_Date_Query object. Date queries are allowed for the user_registered field.

You can generate the date_query array here: https://generatewp.com/wp_date_query/

Example

$date_query = array(
    'relation' => 'AND',
    array(
        'before'        => '2019-05-31',
        'after'         => '2019-01-01',
        'inclusive'     => true,
    ),
);
$users = get_users([
   'date_query' => $date_query
]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.