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.
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.
Share Improve this question asked Mar 30, 2020 at 16:46 Atif AqeelAtif Aqeel 19312 bronze badges 5 |1 Answer
Reset to default 0i 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 ).
__not_in
like the plague, they're incredibly slow – Tom J Nowell ♦ Commented Mar 30, 2020 at 16:56posts_per_page
, are you showing only 10 users, or do you have pagination? – Tom J Nowell ♦ Commented Mar 30, 2020 at 21:15gold
and thefree
roles explicitly, and remove therole__not_in
parameter. I also notice there is no pagination parameter in your query – Tom J Nowell ♦ Commented Mar 31, 2020 at 0:31