admin管理员组文章数量:1390880
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 ).
本文标签: wp queryHow to display user order by role
版权声明:本文标题:wp query - How to display user order by role 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744618449a2615876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
__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