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>';
?>
Here’s the solution. Comments included:
To get all users ordered by a custom meta field, set the
meta_key
and orderbymeta_value
ormeta_value_num
in this case.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