add_action('bp_ajax_querystring','bpdev_exclude_users',20,2); | |
function bpdev_exclude_users($qs=false,$object=false){ | |
//list of users to exclude | |
if($object!='members')//hide for members only | |
return $qs; | |
$excluded_user=join(',',bpdev_get_subscriber_user_ids());//comma separated ids of users whom you want to exclude | |
$args=wp_parse_args($qs); | |
//check if we are searching for friends list etc?, do not exclude in this case | |
if(!empty($args['user_id'])) | |
return $qs; | |
if(!empty($args['exclude'])) | |
$args['exclude']=$args['exclude'].','.$excluded_user; | |
else | |
$args['exclude']=$excluded_user; | |
$qs=build_query($args); | |
return $qs; | |
} | |
function bpdev_get_subscriber_user_ids(){ | |
$subscribers= get_users( array( 'role' => 'subscriber', 'fields' => 'ID' ) ); | |
return $users; | |
} |
Thank you sbrajesh for sharing this code, and unuigbee for the adjustments for multiple roles. It now works awesome. However, it needed a mod that I literally spent half day trying to figure out (which just proves how green I am sometimes) :) so I post this for the next 'copy and paste' coder who is looking for this fix.
I could not get it to work until I changed function bpdev_get_user_ids()
in the multiple roles part to
function bpdev_get_subscriber_user_ids()
which then matched what was in the original code.
The multiple roles thing was huge for me, so I really appreciate that. I'm really not sure if I should have changed the other part instead (removing the word subscriber), but once I got it to work, I didn't dare touch it again. :) If it's a big deal and I should change it, please let me know.
Thanks all.
Has anyone made this work by hiding users based on Paid Memberships Pro level?
Ex: hide users with membership level 1,2,3 from BuddyPress Member Directory
Would anyone be able to help me with modifying this code so that all user roles are hidden, except for users with a specified user role?
Super late reply, but for @Hastibe and anyone else who wants to exclude all but one role, change the exclude to include.
// Helper for fetching all IDs to include. Returns an array in this example.
$included_user_ids = my_function_to_fetch_all_users_by_role();
$args = wp_parse_args( $qs );
$args['include'] = implode( ',', $included_user_ids );
$qs = build_query( $args );
Thanks, sbrajesh, for this. It's exactly what I needed!
Thanks, unuigbee for catching this:
As well as your suggestion for excluding multiple member types.