How can I include user meta information in the resulting array of a WP_User_Quer
-
I’m trying to accomplish some fairly complex custom functionality on a custom WordPress site. The client has an ‘About Us’ page that includes a list of all team members and their pictures. We’re trying to set up the site so that all the client needs to do to add or remove a team member from the About page, would be to add or remove a site user. Basically, the About page auto-populates with WordPress users of a certain user level. I think this was a really clever idea and it’s working great! Here’s the code (in functions.php):
function query_team(){ $admin_query = new WP_User_Query( array( 'role' => 'administrator', ) ); $admins = $admin_query->get_results(); $editor_query = new WP_User_Query( array( 'role' => 'editor' ) ); $editors = $editor_query->get_results(); $author_query = new WP_User_Query( array( 'role' => 'author' ) ); $authors = $author_query->get_results(); $contr_query = new WP_User_Query( array( 'role' => 'contributor' ) ); $contrs = $contr_query->get_results(); $users = array_merge($admins, $editors, $authors, $contrs); return $users; }
As you can see, I’m combining the array results of several WP_User_Queries, since Google informed me that was the only way to get ONLY users of a few pre-defined user groups.
Now, I’ve encountered a bit of a problem. The client (rightfully) wants to be able to sort the users in the order he wants. So I added some custom user meta information called ‘sort_order’, the idea being that you could input a number and then WordPress would sort the display order of the user list based on that number. However, the problem is that these queries don’t actually GET user meta information – the ‘sort_order’ field isn’t included in any of the results from these queries.
To top it all off, it’s not enough to sort the users by the meta information when we make the query; we need to sort them AFTER the arrays have been merged (because an Administrator might have a lower sort_order than an author, for example). So I need a way to include the user meta information in the array that WP_User_Query spits out, merge all the arrays, and then sort that final array using the user meta info.
TL;DR: how can I include user meta information in the resulting array of a WP_User_Query?
Thanks in advance for any assistance!
- The topic ‘How can I include user meta information in the resulting array of a WP_User_Quer’ is closed to new replies.