Help with a custom query: how to sort by a usermeta field
-
Hey there, I am a MySQL newbie (but ok at php) and I found a custom query online to create a list of all site users. I modified it to show only the users whose birthday is this month (‘birthday’ and ‘birthmonth’ are custom fields I added to the usermeta table.) It displays great, the only thing I can’t get it to do is sort by birthday (day of the month, a number.) Right now it’s sorting by first name. Can anyone help? Here is the code:
`$userSort = “birthday”;
$aUsersID = $wpdb->get_col( $wpdb->prepare(
“SELECT $wpdb->users.ID FROM $wpdb->users ORDER BY %s ASC”
, $userSort ));
$current_month = get_the_time(‘n’);
$current_day = get_the_time(‘j’);
foreach ($aUsersID as $iUserID) :
$user = get_userdata($iUserID);
if ($user->birthmonth == $current_month) :
echo ‘- ‘.ucwords(strtolower($user->first_name.’ ‘.$user->last_name.’ (‘.$user->birthmonth.’/’.$user->birthday)).’)
‘;
endif;
endforeach;`
- The topic ‘Help with a custom query: how to sort by a usermeta field’ is closed to new replies.