0

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!

0

To sort by meta fields you need to use meta_key and meta_value (or meta_value_num):

array(
    'meta_key' => 'sort_order', //This is the custom field to orderby
    'orderby' => 'meta_value_num', //for numerical values
    'role'     => 'Administrator', 
    'fields' => 'all_with_meta',
);

However... This will probably order the results before your merge... so,

instead of separate queries you can use meta_query to get multiple roles: (Get multiple roles with get_users)

(This is untested)

global $wpdb;
$blog_id = get_current_blog_id();

$user_query = new WP_User_Query( array(

 'meta_query' => array(
      'relation' => 'OR',
      array(
        'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
        'value' => 'author',
        'compare' => 'like'
      ),
      array(
        'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
        'value' => 'editor',
        'compare' => 'like'
      )
    )
  //Then add the rest of the query

     'meta_key' => 'sort_order', //This is the custom field to orderby
     'orderby' => 'meta_value_num', //for numerical values
    //'fields' => 'all_with_meta', //which fields to return

) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.