Created
June 28, 2013 15:33
-
-
Save ramseyp/5885589 to your computer and use it in GitHub Desktop.
Sorted the return of a Wp_User_Query by both the last name value and the first name value. This way, if multiple users have the same last name, those particular users are then sorted by first name.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function s25_member_loop() { | |
$args = array( | |
'role' => 'member', // the role we're targeting | |
'exclude' => '1', // exclude admin | |
'fields' => 'all_with_meta', | |
'meta_key' => 'last_name', //query on the last_name key | |
'meta_key' => 'first_name', // also the first_name key | |
); | |
$membersquery = new WP_User_Query( $args ); | |
$members = $membersquery->get_results(); | |
// Sort $members by last name; if last name is the same, sort by first name | |
uasort( $members, function ( $a, $b ){ | |
$a->first_name = strtolower( $a->first_name ); | |
$a->last_name = strtolower( $a->last_name ); | |
$b->first_name = strtolower( $b->first_name ); | |
$b->last_name = strtolower( $b->last_name ); | |
if ( $a->last_name == $b->last_name ) { | |
if( $a->first_name < $b->first_name ) { | |
return -1; | |
} | |
else if ($a->first_name > $b->first_name ) { | |
return 1; | |
} | |
//last name and first name are the same | |
else { | |
return 0; | |
} | |
} | |
return ( $a->last_name < $b->last_name ) ? -1 : 1; | |
}); | |
echo '<div id="my_members">'; | |
foreach ($members as $member) : | |
// Do Something with the returned array of users | |
endforeach; | |
echo '</div>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment