1

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>';


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

2 Answers 2

2

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>';


?>
0

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

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.