I am trying to use a WordPress User Query to create a list of users that is ordered by a custom meta value. Its a simple numeric value, going from 1 to 100, so 1 needs to be displayed first, 100 last, etc.
This is my attempt, which has failed miserably:
<?php
$args = array(
'role' => 'Author',
'meta_key' => 'order-number',
'orderby' => 'order-number',
'order' => 'asc',
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors))
{
echo '<div style="float:left;">';
foreach ($authors as $author)
{
$author_info = get_userdata($author->ID);
echo '<div class="post team-member"><div class="image">';
echo get_avatar( $author_info->ID, 91 );
echo '</div>';
echo '<div class="content"><div class="title"><a href="/author/' . $author_info->user_login . '">' . $author_info->user_firstname . ' ' . $author_info->user_lastname . '</a></div>';
echo '' . substr( get_the_author_meta('user_description',$author_info->ID) , 0 , 100 ) . '...';
echo '</div></div>';
}
echo '</div>';
} else {
echo 'No authors found';
}
?>
I think the problem is that wp_user_query does not support custom fields in orderby – so I need a solution that works around this.
Any ideas?
Correct. The orderby parameter can only accept possible values of ‘login’ (default), ‘nicename’, ’email’, ‘url’, and ‘registered’.
A dirty fix would be something like this:
This is largely untested, so I can’t guarantee this will work straight out of the gate, but it should get you started.
Hope this helps!