Title: Order users by meta
Last modified: September 14, 2020

---

# Order users by meta

 *  [candell](https://wordpress.org/support/users/candell/)
 * (@candell)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/order-users-by-meta/)
 * I have created some meta fields on users, one specific field being ‘business_name’.
 * I want to produce a list of all users with the user role ‘business’ ordered by
   this meta ‘business_name’.
 * I was ordering by display_name but many businesses have different worded names,
   hence the new business_name field.
 *     ```
       $users = get_users(array(
                   'role' => 'business',
                   'orderby' => 'display_name',
                   ));
       ```
   
 * Can anyone help ordering by this meta?
 * Also, is it possible to break at each letter, so I can have anchors at the top
   of the page linking to each initial letter? i.e.
 * User selects letter
 * A B C D E F G etc
 * The page lists the users in alphabetical order
 * A
 * A company
    Another Company
 * B
 * Brilliant Company
 * C
 * Creampie
 * Thanks

Viewing 1 replies (of 1 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/order-users-by-meta/#post-13416256)
 * Include these args in get_users() array:
 *     ```
       'role'=>'business',
       'meta_key'=>'business_name',
       'orderby'='meta_value',
       ```
   
 * Ref: [https://developer.wordpress.org/reference/classes/WP_User_Query/prepare_query/](https://developer.wordpress.org/reference/classes/WP_User_Query/prepare_query/)
 * When you loop through the results, at the end of the loop, save the first letter
   of the business name into a variable. At the start of the loop, compare the current
   first letter with that saved in the variable. If they do not match, output a 
   HTML heading element with a related ID so users can jump to that position.
 * To avoid undefined variable warnings, initialize the first letter variable before
   the loop starts. Set it to a char that will not be part of a business name so
   just before the first business is output, a heading will be output, typically
   the “A” heading.

Viewing 1 replies (of 1 total)

The topic ‘Order users by meta’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [5 years, 10 months ago](https://wordpress.org/support/topic/order-users-by-meta/#post-13416256)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
