Order users by custom user meta

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; ?>" rel="nofollow noreferrer noopener" 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.'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" id="dir-id-'.$dir_id.'">'.$dir_name.'</a>';
            echo '</li>';


        } 

        echo '</ul>';


?>

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.'" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" id="dir-id-'.$dir_id.'">'.$dir_name.'</a>';
            echo '</li>';


        } 

        echo '</ul>';


?>

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x