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);
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);
Share
Improve this question
asked Aug 21, 2014 at 10:23
JohnJohn
272 silver badges10 bronze badges
2 Answers
Reset to default 1EDIT
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.
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/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
]);