• 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

    (@bcworkz)

    Include these args in get_users() array:

    'role'=>'business',
    'meta_key'=>'business_name',
    'orderby'='meta_value',

    Ref: https://developer.www.ads-software.com/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.