Order by first name

I’m trying to order the member list by display_name or first name ASC.
http://sim.is/sim/felagatal-sim/

This doesn’t seem to work. Any pointers out there? :/

$users = get_users(array(
    'role' => 'sm_flagar',
    'orderby' => 'display_name',
    'order' => 'ASC',
));

    foreach ($users as $user) {
        $firstName = get_user_meta($user->ID, 'first_name', true);
        $lastName = get_user_meta($user->ID, 'last_name', true);
            echo '<li><a href="' . $user->user_url . '">' . $firstName . ' ' . $lastName . '</a></li>' . PHP_EOL;
    }

Related posts

Leave a Reply

5 comments

  1. Here is my solution

    function split_names( $name ) {
        $names = explode( " ", trim( join( " ", $name ) ) );
        $first_name = array_shift( $names );
        $last_name = array_pop( $names );
        $middle_name = join( " ", $names );
    
        return  array( "first_name" => $first_name, "middle_name" => $middle_name, "last_name" => $last_name );
    }
    
    function sort_by_full_name( $arr1, $arr2 ) {
        $arr1 = split_names( $arr1 );
        $arr2 = split_names( $arr2 );
        $retval = strcoll( $arr1['first_name'], $arr2['first_name'] );
        if ( ! $retval ) $retval = strcoll( $arr1['last_name'], $arr2['last_name'] );
        if ( ! $retval ) $retval = strcoll( $arr1['middle_name'], $arr2['middle_name'] );
        return $retval;
    }
    
    $users = get_users( array(
        'role' => 'sm_flagar',
        'orderby' => 'display_name',
        'order' => 'ASC'
    ) );
    
    $user_list = array();
    
    foreach ( $users as $user ) {
        $firstName = get_user_meta( $user->ID, 'first_name', true );
        $lastName = get_user_meta( $user->ID, 'last_name', true );
        $user_list[] = split_names( array( 
                "first_name" => $firstName, 
                "last_name" => $lastName, 
                "url" => $user->user_url 
        ) );
    }
    
    usort( $user_list, 'sort_by_full_name' );
    setlocale( LC_COLLATE, $locale );
    $locale = setlocale( LC_COLLATE, 'is_IS.utf8' );
    
    foreach ( $user_list as $user ) {
            echo '<li><a href="' . $user["url"] . '">' . 
                        $user["first_name"] . ' ' .
                        $user["middle_name"]
                        $user["last_name"] . '</a></li>' . PHP_EOL;
    }
    
  2. The original code is not so bad. It just is missing the meta_key parameter. Example:

    $theusers = get_users( array(
        'role'     => 'contributor',
        'meta_key' => 'first_name',
        'orderby'  => 'meta_value',
    ) );
    
  3. Nowadays you can do something like this, as long as you’re trying to sort via the default WP usermeta fields:

    $users = get_users(array(
        'fields' => 'all_with_meta'
        // Add whatever arguments you need
    ));
    
    // Sort by first name, ascending
    usort($users, create_function('$a, $b', 'if($a->first_name == $b->first_name) { return 0;} return ($a->first_name > $b->first_name) ? 1 : -1;'));
    
  4. remove ‘,’ (without quotes) after ‘ASC’. It will do the trick.

    $users = get_users(array(
        'role' => 'sm_flagar',
        'orderby' => 'display_name',
        'order' => 'ASC'
    ));
    
        foreach ($users as $user) {
            $firstName = get_user_meta($user->ID, 'first_name', true);
            $lastName = get_user_meta($user->ID, 'last_name', true);
                echo '<li><a href="' . $user->user_url . '">' . $firstName . ' ' . $lastName . '</a></li>' . PHP_EOL;
        }
    

    It is working as it is asked to be. If you echo out the display name of each user in the loop, then you will understand what is going on.

    replace the ‘echo’ line with:

    echo '<li><a href="' . $user->user_url . '">' . $firstName . ' ' . $lastName . '</a>' . $user->display_name . '</li>' . PHP_EOL;
    

    It is not ordering by first and last name but by “display name”.

  5. To get all users ordered by first name, set the meta_key and orderby meta_value.

    $users = get_users( array(
        'role'       => 'director',
        'meta_key'   => 'first_name',
        'orderby'    => 'meta_value',
        'order'      => 'ASC'
    ) );
    

    EDIT: This appeared to work and SHOULD work. I believe it will work in the next version. But for now, it does not work.

    The chosen answer here is the best option I have found until that time:
    Ordering users of a specific role by last name