Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
<?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.

share|improve this question
    
Mabye this answer can help? –  birgire Nov 4 at 18:22
    
@birgire I though of that answer, just couldn't figure out where and who posted it ;-). –  Pieter Goosen Nov 4 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 Nov 4 at 18:47

3 Answers 3

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.

share|improve this answer
    
The OP want it ordered by the include array and not the ID. –  Warface Nov 4 at 14:41

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>';
}
share|improve this answer

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.

share|improve this answer
    
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 –  Pieter Goosen Nov 4 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 Nov 4 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() –  Pieter Goosen Nov 4 at 18:11
    
Or you can have a look at this answer by @birgire. –  Pieter Goosen Nov 4 at 18:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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