Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to exclude Subscribers from a user list. My code is:

$users = $wpdb->get_results( "SELECT display_name FROM $wpdb->users ORDER BY display_name ASC" );

How can I change that so subscribers are excluded from the list?

I also looked at the get_users function in codex but it did not have an exclude by role parameter. 

Edit: Or another way would be to get the results filtered by capability (one that subscribers doesn't have).

share|improve this question
add comment

2 Answers

This new method should work. It looks at all the capability values to find "subscriber", and hides it.

Try this query

$wpdb->get_results(
    "SELECT display_name 
    FROM $wpdb->users WHERE 1=1 AND {$wpdb->users}.ID IN (
        SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta 
        WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}capabilities'
        AND {$wpdb->usermeta}.meta_value NOT LIKE '%subscriber%') ORDER BY display_name ASC" 
);
share|improve this answer
1  
Thank you but I'm looking for a safe solution. This is for a plugin development. –  Mark Nov 14 '13 at 16:08
    
Try that query instead. It should be safe to use unless Wordpress removes capabilities (which probably won't happen lol) –  Howdy_McGee Nov 14 '13 at 17:26
    
interesting! I do not want it in a function. If you look at my own code, I need the results in one line for the $users variable. Can you plz update your question with this? –  Mark Nov 14 '13 at 17:36
    
My mistake, when I read "User List" i thought user list from admin panel. I updated my answer, hopefully this works. –  Howdy_McGee Nov 14 '13 at 17:41
1  
I'm sorry but I really can't say. I'm glad it worked and maybe if left open somebody else will come with a better query than mine. –  Howdy_McGee Nov 14 '13 at 18:07
show 1 more comment

You can use the get_users() function twice to exclude the subscribers.

You can do something like this

// Gets all the subscribers
$user_query = get_users( array( 'role' => 'subscriber' ) );
// This gets the array of ids of the subscribers
$subscribers_id = wp_list_pluck( $user_query, 'ID' );
// Now use the exclude parameter to exclude the subscribers
$non_subscribers = get_users( array( 'exclude' => $subscribers_id ) );

$non_subscribers contains the non subscribers.

You can fetch the display_name by doing something like this

foreach( $non_subscribers as $non_subscriber ) {
    echo $non_subscriber->display_name;
}
share|improve this answer
    
isnt running get_users twice bad for performance? Which way is better for performance, yours or the query posted in the other answer? –  Mark Nov 14 '13 at 23:19
    
get_users internally runs the query, so i guess if you can get something out with running the query once is better than getting it from two queries. But i am not sure if the other query is better than using get_users twice. Lets see if someone enlightens us regarding this :) –  Maruti Mohanty Nov 15 '13 at 6:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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