List users by last name in WP_User_Query

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
    }
} 
?>

Leave a Comment