0

I basically do this now:

$users = get_users();
sort($users);
foreach ( $users as $user ) {
    echo '<option>' . $user->companyname . '</option>';
}

Except the sort() function will sort by the ID. How can I change it to sort by my custom field companyname?

1
  • 1
    Have you read the documentation for get_users?
    – Milo
    Oct 6, 2015 at 21:12

1 Answer 1

1

By passing orderby and order arguments in array.

$args = array( 'orderby' => 'login', 'order' => 'ASC', ); get_users( $args );

orderby - Sort by 'ID', 'login', 'nicename', 'email', 'url', 'registered', 'display_name', 'post_count', 'include', or 'meta_value' (query must also contain a 'meta_key' - see WP_User_Query).

More details : https://codex.wordpress.org/Function_Reference/get_users

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.