$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>users - meta_query orderby sort multiple keys|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

users - meta_query orderby sort multiple keys

matteradmin9PV0评论

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>';
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far