sort wordpress user array by last_name

Hello,

I am trying to query Wordpress users by 4 different custom field types, combine the 4 results with an array merge, and sort them by last_name. So far what happens is that everything comes out of my array by the order the array went into the merge, then first name

$full_query = new WP_User_Query( array( 'meta_key' => 'member_level', 'meta_value' => 'full' ) );
	$loa_query = new WP_User_Query( array( 'meta_key' => 'member_level', 'meta_value' => 'loa' ) );
	$cert_query = new WP_User_Query( array( 'meta_key' => 'member_level', 'meta_value' => 'cert' ) );
	$senior_associate_query = new WP_User_Query( array( 'meta_key' => 'member_level', 'meta_value' => 'senior_associate' ) );
	 
	 $full = $full_query->get_results();
	 $loa = $loa_query->get_results();
	 $cert = $cert_query->get_results();
	 $senior_associate = $senior_associate_query->get_results();
	 
	 $final_users = array_merge($full, $loa, $cert, $senior_associate);
	 
	 
	foreach ( $final_users as $my_users ) {

 echo $my_users->first_name; 
 echo $my_users->last_name;
 echo $my_users->member_level;

    }

Open in new window


so my results come out as so...


first_name          last_name           member_level

Zornitza            Petrova                  full            
Zhenguo             Sun                     full            
Andrew             Anderson               loa            
Alan             Cameron                     loa
Yasmine             Abdel-Hay            cert            
Yvonne             Yeung                    cert            
Andrew J             Lakatos               senior_associate            
Antonia P             Kastanis                senior_associate      


I would like to sort my array_merge by last_name
LVL 1
jblayneyAsked:
Who is Participating?
 
gr8gonzoConsultantCommented:
Add this:
usort($final_users, function($a, $b)
{
    if ($a->last_name == $b->last_name) return 0;
    if ($a->last_name > $b->last_name) return -1;
    return 1;
});

Open in new window


Before this line:
foreach ( $final_users as $my_users ) {

Open in new window

0
 
Julian HansenCommented:
Why are you running multiple queries? Why not run one query that gets ALL the results and sorts them for you?
$full_query = new WP_User_Query( array( 
  'meta_key' => 'member_level', 
  'meta_value' => array('full','loa','cert','senior_associate'),
  'orderby' => 'last_name',
  'order' => 'DESC'
) );
$full = $full_query->get_results();
foreach ( $full as $my_users ) {
  echo $my_users->first_name; 
  echo $my_users->last_name;
  echo $my_users->member_level;
}

Open in new window

0
 
jblayneyAuthor Commented:
thank you for the help, i would actually like to make one query and I tried your code Julian.. The weirdest thing though.. it sorts first all the people with an "A" first name, then sorts by last name.. then the "B" first names and so on...


gr8gonzo, that worked great. i just needed to switch the greater than sign to make it ascending rather then descending
f ($a->last_name < $b->last_name) return -1;
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Julian HansenCommented:
thank you for the help, i would actually like to make one query and I tried your code Julian.. The weirdest thing though.. it sorts first all the people with an "A" first name, then sorts by last name.. then the "B" first names and so on...
The right way of doing this is with a single query - always let the DB do the work if you can.
The problem with the earlier post was that it did not include the meta_query to order by last_name - I have amended and this should do the trick


$full_query = new WP_User_Query( array( 
  'meta_key' => 'member_level', 
  'meta_value' => array('loa', 'cert','senior_associate','full'),
  'orderby' => 'last_name_clause',
  'order' => 'DESC',
  'meta_query' => array(
    'last_name_clause' => array (
      'key' => 'last_name',
      'compare' => 'EXISTS'
    )
  )
  
) );
$full = $full_query->get_results();

foreach ( $full as $my_users ) {
  echo $my_users->first_name . ' '; 
  echo $my_users->last_name . '<br>';

}

Open in new window

1
 
jblayneyAuthor Commented:
thank you, that works great
0
 
Julian HansenCommented:
Do you want to regrade or leave as is?
0
 
jblayneyAuthor Commented:
I cant find any way to do that. Im happy to give you points, just don't know how, he also answered the question as I asked it
0
 
Julian HansenCommented:
I would need to do it but lets leave it.

My only reason for asking was for the sake of the PAQ - if anyone finds this question the accepted answer is not the optimal one.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.