0

I'm trying to read the birthdays of the users in my database through custom fields that have been added. Displaying the current months is easy but I am trying to capture the current month(remainder) and capture part of the next month (10-15 days) based on the current day. I don't understand how to order by the current months and days so they are in ascending order. Not sure if the issue is something in my arguments or something I should be doing in my foreach loop. I could sure use some help trying to understand where I'm going wrong or if my approach is wrong. Thank You.

$current_month = date( 'n' ); // 1-12
$current_day   = date( 'j' ); // 1-31    
$next_month = date('n', strtotime('next month'));
$day_span = 28 - $current_day;

$args_birthday_current = [
'role__in' => ['subscriber'],
//'order' => 'ASC',
'orderby' => ['birth_day' =>'ASC', /*'birth_month' =>'ASC'*/],
'meta_query' => array(

    'relation'  => 'OR',

    array( 'relation'  => 'AND',

        'birth_month' => array(
            'key'       => 'birth_date_month',
            'value'     => $current_month,
            'compare'   => '=',
        ),

        'birth_day' => array(
            'key'       => 'birth_date_day',
            'value'     => $current_day,
            'compare'   => '>=',
        ),

    ), 

    array( 'relation'  => 'AND',

        'next_month' => array(
            'key'       => 'birth_date_month',
            'value'     => $next_month,
            'compare'   => '=',
        ),

        'day_span' => array(
            'key'       => 'birth_date_day',
            'value'     => $day_span,
            'compare'   => '>=',
        ),

    ),
), 
];

My Loop

foreach ( $user_query->get_results() as $user ) {

    $birth_date_month_number = get_user_meta($user->ID, 'birth_date_month', true);
    $birth_date_day_number = get_user_meta($user->ID, 'birth_date_day', true);
    $birthday = date('F jS', mktime(0, 0, 0, $birth_date_month_number, $birth_date_day_number, 0));

    $user_id = "user_".$user->ID;
    $badge = get_field('user_profile_photo', $user_id);
    echo '<a class="profile-link" href="profile-page/user/index.php?user=' . $user->last_name . '"><div>';
    echo '<div class="thumbnail-wrapper"><img class="img-fluid small-thumb" src="' . $badge['sizes']['thumbnail'] . '" alt="' . $badge['alt'] . '" /></div>';     
    echo '<div class="profile-wrapper">' . $user->first_name . ' ' . $user->last_name . '<br>' . $birthday . '</div>';
    echo '</div></a>';
}
4
  • 1
    Holy moly that's going to be one expensive query
    – Tom J Nowell
    Commented Feb 18, 2019 at 20:01
  • 1
    It seems like storing the full birthday as one MySQL timestamp field would simplify everything. Then you could pull all the "subscriber" users, and use PHP for figuring out what users are in the "current month" to display there, and then "next month" to display there, and so on.
    – WebElaine
    Commented Feb 18, 2019 at 21:24
  • WebElaine. Could you expand on your comment. I'm looking to learn and I would be open to hearing your idea further and seeing an example of the pho code. The first question I would ask as someone learning is how do I setup the users page to capture the meta data to be stored as a time stamp. Any links or examples would be much appreciated. Thank You. Commented Feb 19, 2019 at 14:48
  • Tom, when you say one expensive query does that mean like time intensive against the database. What does your statement mean? Thank You. Commented Feb 19, 2019 at 14:49

0

Your Answer

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

Browse other questions tagged or ask your own question.