3

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

5 Answers 5

2

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
  • This works perfect! Your a genius Egill!
    – ingvi
    Commented Sep 27, 2011 at 6:52
  • slight modification to sort by middle names as well (and I had to fix it for the url as well) Commented Sep 27, 2011 at 9:24
7

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',
) );
2

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;'));
0

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".

2
  • sorry it didn't work :/
    – ingvi
    Commented Sep 22, 2011 at 9:58
  • ok, this works like you say :) - all the users have their usernames as display_name. What I want is to order by first_name, got any clue how I can do that? Cheers
    – ingvi
    Commented Sep 23, 2011 at 6:46
0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.