My WordPress users have a custom birthday meta key (DD/MM/AAAA, e.g. 25/02/2020).
If I want to get all users born today (25/02) I can use:
$todays_birthday='25/02/';
get_users(array('meta_compare'=>'like', 'meta_key'=>'birthday', 'meta_value'=>$todays_birthday));
It works perfectly and returns all users born today.
My question is: what if I want to get all users born in the whole week? E.g.:
$todays_birthday=array('24/02/', '25/02/', '26/02/', '27/02/', '28/02/', '29/02/', '01/03/');
What would my get_users query be?
I've tried all of the solutions I've found online, but none worked, they all returned nothing.
My WordPress users have a custom birthday meta key (DD/MM/AAAA, e.g. 25/02/2020).
If I want to get all users born today (25/02) I can use:
$todays_birthday='25/02/';
get_users(array('meta_compare'=>'like', 'meta_key'=>'birthday', 'meta_value'=>$todays_birthday));
It works perfectly and returns all users born today.
My question is: what if I want to get all users born in the whole week? E.g.:
$todays_birthday=array('24/02/', '25/02/', '26/02/', '27/02/', '28/02/', '29/02/', '01/03/');
What would my get_users query be?
I've tried all of the solutions I've found online, but none worked, they all returned nothing.
Share Improve this question asked Feb 25, 2020 at 12:43 otpabuotpabu 112 bronze badges 1 |1 Answer
Reset to default 2First, can use meta_query
, but, you should change your values to use the proper date format, e.g. 2020/01/02 for January 2nd, rather than a regional format. See https://en.wikipedia/wiki/ISO_8601 for more details.
Second, your LIKE
query is going to be very expensive, and achieving what you want while still using a LIKE
query will be even more expensive. So, store a second user meta that contains the month and day, but always sets the year to the same value
Then, do something similar to this:
get_users( [
'meta_query' => [
[
'meta_key' => 'birthday_no_year',
'compare' => 'BETWEEN',
'meta_value' => [ '2000/01/01', '2000/02/01' ],
'type' => 'DATE'
]
]
]);
Caveats:
- You'll need to write code to store
birthday_no_year
values too - You'll need to add these values for existing users
- Searching for objects that have a particular meta value is very expensive, whether it's searching/filter posts by their post meta, or filtering users by their user meta. It doesn't scale, and it can get so expensive that it brings down servers. Cache this and look for alternative ways to store the data.
Legal Consequences
And finally, users should opt-in, this functionality may be considered a leak of personally identifiable information, revealing a users date of birth. This runs foul of numerous data protection and privacy laws, particularly in Europe. I recommend consulting with a lawyer, especially if you're intending to expose the age of the person.
get_users
. Also, you should use a standard date/timestamp format which will improve the number of options available to you. In particular, en.wikipedia/wiki/ISO_8601 indicates you should use YYYY/MM/DD to avoid ambiguity and conform to the internationally agreed date format. DD/MM/YYYY and MM/DD/YYYY are not valid. By storing properly formatted dates, you could then take advantage of the databases date comparison routines – Tom J Nowell ♦ Commented Feb 25, 2020 at 12:56