BuddyDev

Search

Exclude Users from Members directory on a BuddyPress based social network

Hi All,

I hope you all are busy building the next awesome social network.

It has been a long time since I wrote my last post. I was busy setting up an office here in Mohali,Punjab. Everything is setup now, and I am almost back to regular work.

Today, we will see how to exclude some members from  the members directory of BuddyPress. I have seen this question numerous times  and finally I thought to put a small tutorial. It is quick and easy tutorial, so Let us begin.

We will need to hook to 'bp_ajax_querystring' filter.

The following code will allow to exclude the users from the members directory. They will be still listed in the friends list of other users with whom they are friends with.


add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
function bpdev_exclude_users($qs=false,$object=false){
 //list of users to exclude

 $excluded_user='1,2,3';//comma separated ids of users whom you want to exclude

 if($object!='members')//hide for members only
 return $qs;

 $args=wp_parse_args($qs);

 //check if we are listing friends?, 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;

}

You can put the above code in functions.php of your theme or in bp-custom.php.

Everything is fine and the users will be gone from the list. Now, comes a decision point. do you want them to be searchable ? The above code will hide them from the search too.

So, let us improve on our code .


add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
function bpdev_exclude_users($qs=false,$object=false){
 //list of users to exclude

 $excluded_user='1,2,3';//comma separated ids of users whom you want to exclude

 if($object!='members')//hide for members only
 return $qs;

 $args=wp_parse_args($qs);

 //check if we are searching  or we are listing friends?, do not exclude in this case
 if(!empty($args['user_id'])||!empty($args['search_terms']))
 return $qs;

 if(!empty($args['exclude']))
 $args['exclude']=$args['exclude'].','.$excluded_user;
 else
 $args['exclude']=$excluded_user;

 $qs=build_query($args);

 return $qs;

}

That’s it. All we are doing is checking if the query string is for listing members and we modify it to exclude the users.

I hope the code will help a couple of you.

Looking forward to hear your feedbacks :)

Get latest WordPress & BuddyPress tutorials, news and tricks in your inbox.

We send out a lovely email newsletter with the most popular stories. Be a part of it.

55 Responses to Exclude Users from Members directory on a BuddyPress based social network

  • Working. It’s another good addition for our xtrabox services. Thanks Brajesh.

    Would it be possible to include code to hide Super-Admin
    (the first user/admin while installing WP/BP) in our private networks?!?

    Til now I’m using PhpMyAdmin to set user_status from 0 to 99 to hide it.

    • Hi Hans,
      Thank you for the comment.
      I am glad it helped. putting “1″ in the list of excluded users will hide superadmin from the list. Do you want to hide his complete profile btw?

  • Yes, complete.

    What’s now the difference between setting user_status from 0 to 99 (what I’m doing now) and setting «1» with your code?!?

  • Hi,

    Thanks for the post.

    I was wondering if you could help me with taking this a stage further. Rather that individual users, would it be possible to hide an entire group of people based on their role? FOr example show only Authors and exclude subscribers and not make them searchable?

    Regards

    David

  • Thanks for the reply on your other post. This is exactly what I was looking for. Not sure why it isn’t working for me though. I used the first set of code because I don’t want certain members found in search or listed in the member directory. Basically I only want subscribers found. I appreciate any help!

    //Exclude users from Directory and searches.
    add_action('bp_ajax_querystring','bpdev_exclude_users',1,764,769,770,771);
    function bpdev_exclude_users($qs=false,$object=false){
    //list of users to exclude

    $excluded_user='1,764,769,770,771';//comma separated ids of users whom you want to exclude

    if($object!='members')//hide for members only
    return $qs;

    $args=wp_parse_args($qs);

    //check if we are listing friends?, 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;

    }

  • I too was looking for exactly this. Thank you! Unfortunately, I’m having trouble integrating this with s2Member, specifically this line in the “exclude subscribers” version

    $subscribers= get_users( array( ‘role’ => ‘subscriber’ ) );

    I wish to exclude Level 0, or the “Free Subscriber” group. But replacing ‘subscriber’ with ‘Free Subscriber’ doesn’t work, and I don’t know what to put instead. Any ideas?

    • Well, I managed to get the line working with the following:

      $subscribers= get_users( array( ‘role’ => __( ‘subscriber’ ) ) );

      However, I noticed that while “Viewing member 1 to 20 (of 42 members)” has the correct number, the tab at the top (All Members) does not. It still says 48, including the 6 free subscribers. How might I fix this? Thanks!

      • Hi Robert,
        for that purpose you will need to filter on this hook ‘bp_get_total_member_count’
        Hope that helps.

        Regards
        Brajesh

  • Hi Brajesh,

    How would I modify the code to show different users in different tabs (or even pages) under buddypress members? As I understand the code now it always removes certain users. Would I add the action in the members-loop.php instead and then remove it again? And create lets say:
    members-students.php, members-teachers.php, members-assistants.php to show different user roles?
    I hope you know what I am trying to achieve….

    • Hi Sascha,
      sorry for the delayed reply. I am unable to understand the goal. are you trying to achive tabls like all members, students, assistants or you are trying to show specific tabs when a user is logged in. In both the cases you can edit members/index.php and use conditionals to achieve it.

      • Hi Brajesh,

        Thanks for your reply, late or not!
        Yes, I am trying exactly that. I achieved some of it. Look at: http://www.landwire.de/members
        But everytime I use the filters (newest, alphabetical, last active) the AJAX request destroys all my tabs. I think the search also returns all users and not only users of a certain role. I tried looking at the AJAX, but got completely lost. Not sure where it queries the DB and how I could filter that….
        If you have a better idea, I would be glad to hear it.

        • Hi Sascha,
          Thank you for the comment. Please allow me till this weekend and I will put a sample child theme of bp-default to show fetching users by role. That will be easier for you to understand and adapt.

          Regards
          Brajesh

      • Could not reply to your post below. But that would be marvellous to see what you will come up with. I used WP UI to create the tabs, so I am not sure if that would interfere with anything. Anyway, it does not necessarily have to be in tabs. Own pages are fine too, which brought me to think to create the different templates, which I did not get round to yet…

  • Hi Brajesh,

    Thank you for your post. I was looking for a way to exclude blogs created by super-admin to appear on the “Blogs” list page from BuddyPress. I landed on this page http://buddypress.org/community/groups/how-to-and-troubleshooting/forum/topic/hide-certain-blogs-from-the-blog-directory/ where someone suggested to look at your code here. I believed it should work as suggested to change line 7 to if($object!=’blogs’) //hide for blogs directory only, but it didn’t work.

    Do you have any idea how to modify your code so it can exclude certain blogs??

    Thanks so much!

    • Hi Corey,
      Thank you for the comment.
      It won’t work for the blog as blogs component does not allow excluding at the moment.
      Here is some code that will do it(but will exclude the blogs from blog lists for you as admin too, you may want to use a conditional like is_super_admin)

      https://gist.github.com/2790977

      hope that helps.
      Regards
      Brajesh

  • I’m trying to use this on a multisite and I get it work work on individual site member directories all except for a subdomain like forums.site.com and it has bbpress. Any reason why it works for any other site in the network but just not this one. It has no plugins activated other than bbpress and it has the same child theme the other sites have.

  • You beauty!!
    I have successfully filtered the members directory to remove members with role s2member_level2 – just what I wanted – using your code.

    I imagine that to filter free suscribers when using s2Member you’d need to put

    $subscribers= get_users( array( ‘role’ => ‘s2member_level0’ ) );

    not

    $subscribers= get_users( array( ‘role’ => ‘subscriber’ ) );

    In caase it’s not clear to anyone how to do this, I added the functions from

    https://gist.github.com/2142009

    to my functions.php file in my theme.

  • hi I added the code, and wanted to hide superadmin using 1, but I still see them on the list, does the code still work?

  • Hi Brajesh,

    I need a little add-on to your exclude-users-code: the tab no. of ALL group tabs should show member count minus 1 (minus the admin I’m hiding). Otherwise these Learning groups are confused on info.ch thinking there is a member what they don’t have… Thanks for help!

  • Hi Brajesh,

    Would it be possible for you to add some code to correct the number of members shown in the Group Tab (x minus the hidden one[s])?

    It’s important for that Learning Group concept I’m using: it’s confusing for them to see a number x+1 (I’m hiding the Admin, Admin is of course not a real Member of the Group).

    • Hi Hans,
      you can filter on ‘bp_get_total_member_count’ to show the correct count.
      here is an example.

      add_filter(‘bp_get_total_member_count’,'bpdev_members_correct_count’);
      function bpdev_members_correct_count($count){
      $excluded_users_count=3;//change it to the no. of user you have excluded

      return $count-$excluded_users_count;

      }

      That will make it work.

      • Hi Brajesh,
        Thanks for your help. While trying to use that code I saw that my Admin account is hidden from the main Members Directory (with the code you supplied earlier) – but NOT from a Group Members Directory. I don’t know why I didn’t notice that earlier. Good to have my own Beta-Phase… ;-)
        Would you please add that function (also hiding account[s] from Group Members Directory)?

  • Hi Brajesh I am still newbie with all hooks and filters so I am looking for your help here.
    I am using the following code to help me with the search.

    current_component == BP_SEARCH_SLUG )//if thids is search page
    bp_core_load_template( apply_filters( ‘bp_core_template_search_template’, ‘search-single’ ) );//load the single searh template
    }
    add_action(“advance-search”,”bpmag_show_search_results”,1);//highest priority

    /* we just need to filter the query and change search_term=The search text*/
    function bpmag_show_search_results(){
    //filter the ajaxquerystring
    add_filter(“bp_ajax_querystring”,”bpmag_global_search_qs”,100,2);
    }

    //show the search results for member*/
    function bpmag_show_member_search(){
    ?>

    total_member_count>1):?>
    <a href="” >total_member_count),”bpmag”);?>

    <a href="” >

    <a href="” >

    <a href="” >

    <a href="” >

    current_component == BP_SEARCH_SLUG)
    return true;
    return false;
    }
    ?>

    The problem here is when search is done without any search terms it still conduct the search and displays all user. How would I stop from displaying all members? or if it can’t be done than I want it to display only friends not all the registered member which are not their friend.

    I would also appreciate your help if you can tell me how to disable http://www.example.com/memebers from showing all the users. I don’t want my users to see all the user in my site.

  • This is the code i think the above mentioned code is cutoff.

    current_component == BP_SEARCH_SLUG )//if thids is search page
    bp_core_load_template( apply_filters( ‘bp_core_template_search_template’, ‘search-single’ ) );//load the single searh template
    }
    add_action(“advance-search”,”bpmag_show_search_results”,1);//highest priority

    /* we just need to filter the query and change search_term=The search text*/
    function bpmag_show_search_results(){
    //filter the ajaxquerystring
    add_filter(“bp_ajax_querystring”,”bpmag_global_search_qs”,100,2);
    }

    //show the search results for member*/
    function bpmag_show_member_search(){
    ?>

    total_member_count>1):?>
    <a href="” >total_member_count),”bpmag”);?>

    <a href="” >

    <a href="” >

    <a href="” >

    <a href="” >

    current_component == BP_SEARCH_SLUG)
    return true;
    return false;
    }
    ?>

  • how to exclude some type of user role members from member loop ?

    • Hi Smith,
      you will need to find the user ids for that role.
      In this case, you can use http://codex.wordpress.org/Function_Reference/get_users to find the ids(note the fields parameter).

      Once you get the ids, you can use the above method.

      • hi thank you for reply me. but in my buddypress site i have two types or role one is ‘role a’ and second is ‘role b’ i do not want to show ‘role b’ type of members in my members page when i writes $user = new WP_User( bp_get_member_user_id() );
        if ( $user->roles[0] != ‘subscriber’ ) :
        ?> this code in member loop.php then it works but problem is it is hiding the member role users from member loop but it is not showing the no of other roles members limit that i have kept for display users per page. for example i have 7 users from that first three user is of role a and fourth and fifth are of role b, i have kept limit 5 users per page. it is showing only first three users of role a and sixth and seven on other page.

  • hi please help me how to exclude certain type of user role members from member loop? to display in member page

  • I have applied your code to my wp, but if press the bp search button with no content or with “admin” as search string, the user still shows up in the search results.

    I did a step by step debug of your code, everything seems to go right, $object contains ‘members’ and the contents of $qs that is returned are ‘Array ( [exclude] => 1 )’.

    And thats where I lose track of this problem

  • Hello.

    I have wordpress version 3.4.2 – buddypress Version 1.6.1 & 3rd party buddypress theme installed.

    Can you assist me in what exact code I would need to ‘hide’ admin user from BuddyPress? I am using the Global Hide Admin bar plugin, so that’s not the problem.

    I would like user 1 (admin) to be in stealth mode with no traces of that profile inside buddypress registered/unregistered.

    Thanks very much!

    • Hi Brandon,
      Though you can easily hide admin from the directory(using above code) and make him inactive by deleting the last_active time , it is not feasible to hide him from Groups at the moment. I have worked with Hans on the similar problem, and we faced a lot of issues.

      If you don’t want the admin profile to be visible for anyone, just use some check on init action and redirect user if they try to visit the admin account.

  • Hi, @sbrajesh, i dont know whats wrong with the code above because it hide admin in member directory and also on friends list

  • Hi, @sbrajesh, i dont know whats wrong with the code above because it hide admin in member directory and also on friends list when you click on friends tab.

  • Hi Brajesh,
    How can I exclude the current loggedin user from members directory and search?
    Thanks

    • Hi Sara,
      Thank you for asking.
      You can use the code below to exclude the current logged in user

      
      add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
      function bpdev_exclude_users($qs=false,$object=false){
       //list of users to exclude
       if(!is_user_logged_in())
           return $qs;
       //if the user is logged in , let us exclude her/him
       $excluded_user=  get_current_user_id();
       
       if($object!='members')//hide for members only
       return $qs;
       
       $args=wp_parse_args($qs);
       
       //check if we are listing friends?, 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;
       
      }
      

      hope that helps.

  • Pingback: Beginner’s Guide To BuddyPress: Tips And Resources

  • Can you provided an updated version of the code, where the excluded user isn’t seen even if he/she is friends (so it’ll be almost impossible to friend him/her) and so that the excluded user isn’t seen even if the user looking for the excluded user is not logged in?

    I don’t think this code works if the user looking for the excluded user is not logged in based off of this comment (//hide for members only), but I may be misinterpreting it.

    • Hi John
      Just remove these two line from the code

       //check if we are listing friends?, do not exclude in this case
       if(!empty($args['user_id']))
       return $qs;
      

      and the user will be hidden from friend lists too.

  • hie i put above code right after <?php and finished it with ; but ddnot worked for me..
    please help me for this brajesh sir
    were exactly to put this code so we can not see friend list of non friends
    thanku again sir

  • How would I exclude 2 user roles? Administrator and Subscriber?

  • Can this be tweaked to prevent listing any members at all on the members page and the search? I don’t want anybody listed, ever, no matter what. I’ve been scratching my head about how to do it. Thanks!

    • Hi kenny,
      You don’t need this in that case.
      here is a simple solution

      add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
      function bpdev_exclude_users($qs=false,$object=false){
       
       if($object!='members')//hide for members only
       return $qs;
       if(bp_is_members_component()&&!bp_is_user()){
           
           return 'include=0';
       }
       
       return $qs;
       
      }
      

      We are returning invalid user id as include causing to hide all members. Can you please try it and let me know if it works for you or not?

  • how can i hide subscriber+admin from member directory ? I’m using this code
    https://gist.github.com/sbrajesh/2142009 to hide subscriber but still admin user shows how to hide them?

  • Hi Brajesh, just passing by to say thank you very much! That was the solution i needed! Grettings from Brasil! You’re a great contributer to the developer’s society!

Leave a Reply

Are you looking for support?

Please join BuddyDev Premium Club to get timely support. At the moment, We are unable to provide much support in comments. By Joining BuddyDev Premium Club, you will get guaranteed support(and we have 30 days money back guarantee too).

Your email address will not be published. All the fields marked as * are required.


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>