0

I have a problem with listing my users, who have a birthday today (for this instance today is March 3rd).

I've created a custom field 'birth_date' using tutorial here with some minor tweaks.

My WP_User_Query doesn't show any users. Here's my code:

    $args = array(
    'meta_key' => 'birth_date',
    'meta_value' => array( 'day' => '03', 'month' => '03')
    );

    // The Query
    $user_query = new WP_User_Query( $args );

    // User Loop
    if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {


    echo '<p>' . $user->display_name . '</p>';

    }
    } else {
    echo 'No users found.';
    }

Here is var_dump of 'birth_date':

    array(1) { [0]=>
    array(3) { 
              ["day"]=> string(2) "03" 
              ["month"]=> string(2) "03" 
              ["year"]=> string(4) "1992" 
              } }

I suspect there is a problem with the way I express 'meta_value' in $args, however my knowledge in PHP is very limited.

2
  • You can't save it as an array if you want to reliably query on that data, it needs to be a single yyyy-mm-dd value.
    – Milo
    Commented Jun 5, 2017 at 15:09
  • @Milo I've figure it out, gonna post my solution in a bit
    – ERDFX
    Commented Jun 5, 2017 at 15:30

1 Answer 1

0

Here is what I did and it worked for me:

$current_day = date("d");
$current_month = date("m");

$args = array(
'orderby' => 'login',
'order' => 'ASC',
);

// The Query
$user_query = new WP_User_Query( $args );

// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {

        if ( $user->birth_date['day'] == $current_day &&
             $user->birth_date['month'] == $current_month )

         { echo '<span>' . $user->display_name . '</span>'; }   

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.