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.

I'm showing a list of users like so:

<ul>

<?php   $directors = get_users('role=director'); 

        foreach ($directors as $director) { 

            $dir_id = $director->ID;

            $dir_order = get_user_meta($dir_id, 'exit_director_order', TRUE);

            $dir_link = get_bloginfo('home').'/?author='.$dir_id; 

            if ($dir_id == $director_id ) {

                $dir_class= 'current director-'.$dir_id;

            } else { 

                $dir_class= 'director-'.$dir_id;
            }
?>

           <li>
                <a href="<?php print $dir_link; ?>" class="<?php print $dir_class; ?>"><?php echo $director->display_name; ?></a><br>
           </li>


<?php } ?>


</ul>

I'd like to order the users by dir_order (in order of smallest number to largest). These values are stored in the database as integers.

How might I go about doing this?


EDIT:

Here's the solution. Comments included:

<?php   $results = get_users('role=director'); 

        foreach ($results as $result) {

            // Get data about each user as an object
            $user = get_userdata($result->ID); 


            // Create a flat array with only the fields we need
            $directors[$user->ID] = array(
                'dir_order'     =>  $user->exit_director_order,
                'dir_id'        =>  $user->ID,
                'dir_name'      =>  $user->first_name.' '.$user->last_name        
            );
        }

        // Sort
        sort($directors); 

        // The list
        echo '<ul id="rightcolumndirector">';

        // For each result
        foreach ($directors as $director) { 

            // Set up the variables
            $dir_id = $director['dir_id'];
            $dir_order = $director['dir_order'];
            $dir_name = $director['dir_name'];
            $dir_link = get_bloginfo('home').'/?author='.$director['dir_id']; 


            // The list items
            echo '<li>';
            echo '<a href="'.$dir_link.'" id="dir-id-'.$dir_id.'">'.$dir_name.'</a>';
            echo '</li>';


        } 

        echo '</ul>';


?>
share|improve this question
    
can I use ksort() ? –  Niels Oct 5 '11 at 3:40
    
Please move your solution to an answer so this doesn't haunt site as unanswered. –  Rarst Oct 12 '11 at 22:03
    
Thanks for the reminder Rarst. I didn't have privileges when this was asked :-) –  Niels Oct 13 '11 at 23:25
add comment

2 Answers 2

up vote 1 down vote accepted

Here's the solution. Comments included:

<?php   $results = get_users('role=director'); 

        foreach ($results as $result) {

            // Get data about each user as an object
            $user = get_userdata($result->ID); 


            // Create a flat array with only the fields we need
            $directors[$user->ID] = array(
                'dir_order'     =>  $user->exit_director_order,
                'dir_id'        =>  $user->ID,
                'dir_name'      =>  $user->first_name.' '.$user->last_name        
            );
        }

        // Sort
        sort($directors); 

        // The list
        echo '<ul id="rightcolumndirector">';

        // For each result
        foreach ($directors as $director) { 

            // Set up the variables
            $dir_id = $director['dir_id'];
            $dir_order = $director['dir_order'];
            $dir_name = $director['dir_name'];
            $dir_link = get_bloginfo('home').'/?author='.$director['dir_id']; 


            // The list items
            echo '<li>';
            echo '<a href="'.$dir_link.'" id="dir-id-'.$dir_id.'">'.$dir_name.'</a>';
            echo '</li>';


        } 

        echo '</ul>';


?>
share|improve this answer
add comment

To get all users ordered by a custom meta field, set the meta_key and orderby meta_value or meta_value_num in this case.

$results = get_users( array(
    'role'       => 'director',
    'meta_key'   => 'exit_director_order',
    'orderby'    => 'meta_value_num',
    'order'      => 'ASC'
) );

EDIT: This appeared to work and SHOULD work. I believe it will work in the next version. But for now, it does not work.

The chosen answer here is the best option I have found until that time: Ordering users of a specific role by last name

share|improve this answer
add comment

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.