Ready to get started?Download WordPress


Help with a custom query: how to sort by a usermeta field (2 posts)

  1. semidivine
    Posted 4 years ago #

    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 '

  2. '.ucwords(strtolower($user->first_name.' '.$user->last_name.' ('.$user->birthmonth.'/'.$user->birthday)).')
  3. ';

  • semidivine
    Posted 4 years ago #

    hey my backticks didn't prevent the link from messing it up... let me try this again. that echo is:

    echo '[li][a href="/?author='.$iUserID.'"]'.ucwords(strtolower($user->first_name.' '.$user->last_name.'[/a] ('.$user->birthmonth.'/'.$user->birthday)).')[/li]

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic