3
<?php
$blogusers = get_users('include=5,6,2,7,12,8');
foreach ($blogusers as $user) {
...         
}
?>

Right now, these users will sorted by first name.

How do I sort this in the order the numbers are in include=...?

Example:

User 5 first, user 6 second, user 2 third etc.

3
  • Mabye this answer can help?
    – birgire
    Commented Nov 4, 2014 at 18:22
  • @birgire I though of that answer, just couldn't figure out where and who posted it ;-). Commented Nov 4, 2014 at 18:39
  • 1
    @PieterGoosen It's great to see so many different solutions for the OP ;-) But your solution (+1) addresses this question directly, whereas the one I linked to, does so only indirectly.
    – birgire
    Commented Nov 4, 2014 at 18:47

3 Answers 3

4

There is a simpler, faster method than the one by @Warface. This is an extention to @SyHolloway

The concept here is to wrap get_users() in a new function in which you can sort the result from get_users() by the include parameter using usort and then return the resulted array sorted by include

This function does not make provision for the fields argument to be used when the include parameter has been set. all is the only value that will work. You can extent the function to make it work with the other values of the fields parameter when include is in use. Otherwise, if include is not set, the fields parameter works as normal.

Naturally, orderby and order will not work when include is used

This new function operates and is used in exactly the same way as get_users(), except for the above mentioned rules

Here is the function

function get_users_by_include( $args = array() ) {
    $blogusers = get_users( $args );

    if( isset( $args['include'] ) ){
        $include = $args['include'];
        usort($blogusers, function ($a, $b) use( $include ){
            $q = array_flip( $include ); 
            return $q[$a->ID] - $q[$b->ID];
        });
    }

    return $blogusers;
}

You can now use this in your template as follows

$users = get_users_by_include(array( 'include' => array(2,5,1,10,45,32,66) ) );
foreach ( $users as $user ) {
    echo $user->ID . '</br>';
}
0

Since WP doesn't respect the array order injected in the include array, I've made a little script to achieve it.

$user_id = array(2,5,1,10,45,32,66);

foreach($user_id as $uid):
    $user_query = get_users(array('role'=>'member', 'number' => 1,'include' => $uid));
    echo $user_query[0]->ID // or display_name like you like...
endforeach;

I know it's kinda lame to query each time the database but hey it works.

4
  • This will work, but you are hitting the db a lot. I still think that you should try to use some form of php sorting on the returned array Commented Nov 4, 2014 at 14:58
  • Yeah but sorting the array don't do anything in the get_users() include parameter. It will order it alphabetically or by IDs. To keep the array order injected, I can't think of any other ways.
    – Warface
    Commented Nov 4, 2014 at 15:02
  • Please see my answer. As I said, you need to make use of php sorting on the returned array from get_users() Commented Nov 4, 2014 at 18:11
  • Or you can have a look at this answer by @birgire. Commented Nov 4, 2014 at 18:43
0

Wordpress returns an array of objects.
Have you tried using 'orderby' as an extra paramiter? eg

<?php
$blogusers = get_users('include=5,6,2,7,12,8&orderby=id');
foreach ($blogusers as $user) {
    //This will loop in the order of id 2,5,6,7,12  
}
?>

Now you know what ID's are in what order, you can use some php array sorting functions

Also some more at php maunual

Its not a total solution but should put you well on your way.

1
  • The OP want it ordered by the include array and not the ID.
    – Warface
    Commented Nov 4, 2014 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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