I am terrified there’s no easy way to do this, and I’ll need to tell my client what they want isn’t possible, but I’m gonna give it a shot here just in case.

I’m building a musician’s networking site that allows users to edit their profiles, then search for other musicians to work with or hire based on a variety of information like their instrument, location, and name.

Because WordPress for some reason doesn’t like combining the meta queries I use to access my custom fields with the regular search function I use to access normal information like login, nicename, email, and url, I was forced to create two different queries and join them together, like so.

// Query against standard WordPress user inputs
$search_query = new WP_User_Query( $args1 );
// Query against my custom user inputs
$meta_query = new WP_User_Query( $args2 );

// Create a blank query to put the two merged queries into
$user_query = new WP_User_Query();

// Merge the two queries.
$user_query->results = array_merge( $search_query->results, $meta_query->results );

//populate total_users count for the loop to work correctly
$user_query->total_users = $search_query->total_users + $meta_query->total_users;

Works like a charm. The queries are then used in a loop with some JQuery to generate the results on the directory page.

Now, though, my client is requesting that the results be returned in alphabetical order, though they haven’t told me which field they’d like me to use. I’m gonna assume it’s either login or last_name.

Is there a way I could grab the $user_query variable with both combined queries in it and organize it alphabetically according to an arbitrary variable? Or maybe there’s a clever way someone can recommend that I can combine them in a specific way so that they’ll be alphabetized.

Thank you!

Read more here: Is it possible to sort the results of a WordPress query AFTER the query has been run?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: