butlerblog
12/22/2016 - 5:37 AM

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 na

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.

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