How can I list users by last name ASC when using WP_User_Query?
There is an orderby parameter but looking in core it doesn’t accept ordering my user meta. Anyone know how to extend WP to allow this ordering by last name?
How can I list users by last name ASC when using WP_User_Query?
There is an orderby parameter but looking in core it doesn’t accept ordering my user meta. Anyone know how to extend WP to allow this ordering by last name?
There is a better way to do this as of WordPress version 3.7. Use the WordPress property meta_key to select the last name property and then orderby => meta_value with an ascending order.
<?php
$args = array(
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$user_query = new WP_User_Query( $args );
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $author ) {
// Line below display the author data.
// Use print_r($author); to display the complete author object.
?>
<a href="https://wordpress.stackexchange.com/questions/30977/<?php echo get_author_posts_url($author->ID); ?>" class="author"><?php echo get_avatar($author->ID, 96); ?><?php echo $author->display_name; ?></a>
<?php
}
}
?>