0

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.

  • 1
    It may be that you need to make multiple queries/calls to 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.org/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 Feb 25 at 12:56
1

First, 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.org/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.

| improve this answer | |
  • Thank you for your response, I'll try and see. As for legal consequences, don't worry, the script is for internal use (if it's your birthday, you get a special offer - that's all). User birthdays are not displayed on any public page. – otpabu Feb 25 at 13:54
  • Ah, just be aware some countries also regulate for internal breaches, not just public – Tom J Nowell Feb 25 at 14:08

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.