admin管理员组文章数量:1427910
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
]);
本文标签: pluginsMeta query for comparing two dates
版权声明:本文标题:plugins - Meta query for comparing two dates 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745494196a2660736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论