1

I am trying to display user on query but i want to display order by user role. Suppose we have 2 user role, Gold and Free. I want to display Gold user first and then free users.

$args = array(
                'order'      => 'ASC',
                'orderby'    => 'display_name',
                'role__not_in' => 'administrator',
                'posts_per_page' => 10,
                'meta_query' => array(
                    'relation' => 'OR',
                    array(
                        'key'     => 'pin_code',
                        'value'   => sanitize_text_field($_GET['pin_code']),
                        'compare' => 'LIKE'
                    ),
                    array(
                        'key'     => 'city',
                        'value'   => sanitize_text_field($_GET['pin_code']),
                        'compare' => 'LIKE'
                    ),
                    array(
                        'key'     => 'county',
                        'value'   => sanitize_text_field($_GET['pin_code']),
                        'compare' => 'LIKE'
                    ),
                )
            );

            // Create the WP_User_Query object
            $wp_user_query = new WP_User_Query($args);

i dont know how to achieve this in query arg.

5
  • It would be a lot faster to display users in the roles gold and free than it would be to show all users except administrators, avoid parameters ending in __not_in like the plague, they're incredibly slow
    – Tom J Nowell
    Commented Mar 30, 2020 at 16:56
  • Got your point. just need to know how to sort by user role
    – Atif Aqeel
    Commented Mar 30, 2020 at 17:24
  • These are the comments, not answers, I see you used posts_per_page, are you showing only 10 users, or do you have pagination?
    – Tom J Nowell
    Commented Mar 30, 2020 at 21:15
  • i do have pagination and each page having 10 user to display
    – Atif Aqeel
    Commented Mar 30, 2020 at 21:30
  • Note that at the moment your code will also pull in contributors, subscribers, authors, editors, etc. You should tell it you want users with the gold and the free roles explicitly, and remove the role__not_in parameter. I also notice there is no pagination parameter in your query
    – Tom J Nowell
    Commented Mar 31, 2020 at 0:31

1 Answer 1

0

i dont know how to achieve this in query arg.

This is because it cannot be done using just arguments. No option for role is listed in the order/orderby parameter docs. There is no option to sort by user role in WordPress.

Instead, you're going to have to query for all gold role users, grab their user IDs, then query for all free users, grab their user IDs, then query for the users with those IDs in a third query.

Notes:

  • This will not scale, if you have a lot of users, this will grind the site to a halt, and the page may not finish loading
  • You will have to throw away your pagination code, and re-implement it to work off of the array
  • You'll need to figure out which IDs in the array are for the current page, and slice it accordingly
  • You'd need to figure out wether the current page is for gold or free users, or if there's an overlap and slice the appropriate array to get the IDs
  • It would be easier to just have separate archives/lists for gold and free users
  • Your code would expose personally identifiable information if it was ever shown to non-employees/staff, making it illegal in numerous countries ( it would violate GDPR in EU states ).

Your Answer

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

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