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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.