1

I have a date of birth user field, I am trying to show recent (past 7 days) and upcoming (next 7 days) birthdays. I have the below but it is not working because the years in the date of births are different to this current year

$today = date( 'Y-m-d' );

$date_add_two_days = date_create();
date_add($date_add_two_days, date_interval_create_from_date_string('7 days'));

$date_sub_two_days = date_create();
date_sub($date_sub_two_days, date_interval_create_from_date_string('7 days'));


$upcoming_birthdays_args = array(
    'orderby' => 'meta_value',
    'meta_key' => 'dob',
    'number' => 999,
    'meta_query' => array(
        array(
            'key' => 'dob',
            'value' => $today,
            'compare' => '>=',
            'type' => 'DATE'
        ),
        array(
            'key' => 'dob',
            'value' => date_format($date_add_two_days, 'Y-m-d'),
            'compare' => '<=',
            'type' => 'DATE'
        )
    )
);

$upcoming_birthdays = get_users( $upcoming_birthdays_args );

$recent_birthdays_args = array(
    'orderby' => 'meta_value',
    'meta_key' => 'dob',
    'number' => 999,
    'meta_query' => array(
        array(
            'key' => 'dob',
            'value' => $today,
            'compare' => '<',
            'type' => 'DATE'
        ),
        array(
            'key' => 'dob',
            'value' => date_format($date_sub_two_days, 'Y-m-d'),
            'compare' => '>=',
            'type' => 'DATE'
        )
    )
);

$recent_birthdays = get_users( $recent_birthdays_args );

How can I make the query only work with the month and date and not the year? Thanks

  • First of all, I think you only need one meta_query array in each query, pass your start and enddate as an array and use BETWEEN as a compare operator. – Michael Sep 18 '18 at 10:32

Your Answer

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

Browse other questions tagged or ask your own question.