Skip to:
Content
Pages
Categories
Search
Top
Bottom

Excluding Roles from the Member's Directory.


  • rianhall
    Participant

    @rianhall

    I am trying to exclude some roles from the Members Directory. I have found several very old topics on this but none of them are working. Can anyone point me in the direction on how to exclude certain roles from the Member’s Directory?

Viewing 16 replies - 1 through 16 (of 16 total)

  • modemlooper
    Moderator

    @modemlooper

    get user id’s of the role and then exclude those id’s. google how to exclude id’s from buddypress members loop. it’s been posted here a few times


    Benoit Hennegrave
    Participant

    @benhen75


    Benoit Hennegrave
    Participant

    @benhen75


    rianhall
    Participant

    @rianhall

    The Code from “SOLVED: Limit Members Loop by Profile Fields” seem easy to implement. I added this to my function.php

    // to exclude only defined roles in the Buddypress Members LOOP
    
    function exclude_by_role($exclude_roles) {
    
    $memberArray = array();
    
    if (bp_has_members()) :
    while (bp_members()) : bp_the_member();
    $user = new WP_User( bp_get_member_user_id() );
    $user_role = $user->roles[0];
    foreach ($exclude_roles as $exclude_role) {
    if ($exclude_role==$user_role) {
    array_push($memberArray, $user->ID);
    break;
    }
    }
    endwhile;
    endif;
    
    $theExcludeString=implode(",",$memberArray);
    
    return $theExcludeString;
    }

    I then added this to my member-loop.php

    <? $excluded_roles = array ('administrator', 'author', 'subscriber'); // this can be any roles you have set up
    	if (bp_has_members( 'exclude=' . exclude_by_role($excluded_roles) . '&' . bp_ajax_querystring( 'members' ) ) ) : ?>
    

    Unfortunately, it still is not excluding any of the specified roles. I took a look at the second example, but not sure where to put the code. Any suggestions?


    Henry
    Member

    @henrywright-1

    Can’t you just pass the ids of the roles you want to bp_has_members() using include?

    You could get the user ids you need using get_users() then do an array_merge()

    Take a look at:

    Members Loop


    rianhall
    Participant

    @rianhall

    I am still having this problem and REALLY need a solution. I’m not a developer so ca anyone walk me through what I need to do?

    so far I have added this to my functions.php

    // to exclude only defined roles in the Buddypress Members LOOP
    
    function exclude_by_role($exclude_roles) {
    
    $memberArray = array();
    
    if (bp_has_members()) :
    while (bp_members()) : bp_the_member();
    $user = new WP_User( bp_get_member_user_id() );
    $user_role = $user->roles[0];
    foreach ($exclude_roles as $exclude_role) {
    if ($exclude_role==$user_role) {
    array_push($memberArray, $user->ID);
    break;
    }
    }
    endwhile;
    endif;
    
    $theExcludeString=implode(",",$memberArray);
    
    return $theExcludeString;
    }
    

    Then added this to my member-loop.php

     
    $excluded_roles = array ('administrator', 'author', 'subscriber'); // this can be any roles you have set up
    	if (bp_has_members( 'exclude=' . exclude_by_role($excluded_roles) . '&' . bp_ajax_querystring( 'members' ) ) ) : 
    

    Unfortunately, no luck. It is not excluding any of the three roles noted above.


    rianhall
    Participant

    @rianhall

    Any one have any suggestions?


    meg@info
    Participant

    @megainfo

    Hi @rianhall,

    add this code to functions.php :

    https://gist.github.com/dzmounir/8285459

    No need to edit any template files. Just change
    $excluded_roles = array( 'administrator', 'subscriber' );

    by puting the role ids to hide.


    rianhall
    Participant

    @rianhall

    Thanks for the code and instructions. Unfortunately, it is still not working. Here is exactly what I did:
    1. Copied the code.
    2. Removed the code I had in the functions.php file and pasted the new code in it.
    3. Reverted my member-loop.php file to it’s original state, free from any customizations.
    4. Logged into my site
    5. Went to the members list.

    Result:
    I am still seeing the Administrator and Subscriber users.

    Any other ideas or am I doing something wrong?


    rianhall
    Participant

    @rianhall

    This is really odd. I must have had something cached, because it it working now. Hmmmm.

    I guess I’ll keep an eye on it, but it does seem to be working. Thanks a million!!!


    somethingelse
    Participant

    @somethingelse

    excluding specific ids isn’t necessarily helpful if you have a regularly changing membership…

    FWIW – i am not a programmer… i am good at patching together workarounds tho 🙂
    somewhere along the way i found this approach to ONLY displaying my level3 members:

    <?php while ( bp_members() ) : bp_the_member(); ?>
    
      <?php
           $wp_user = new WP_User( bp_get_member_user_id() );
          if( $wp_user->roles[0] == 's2member_level3' ): ?>

    which seems to do the trick – this is in members-loop.php

    at the top of that file i have
    <?php if ( bp_has_members( 'type=alphabetical' ) ) : ?>

    it used to be bp_ajax_querystring() which seemed to continually thwart my efforts.

    Now it nicely displays my teachers in alpha order with no headache… granted, it’s by first name, which i will eventually figure out how to fix.

    Obviously i’m using s2member with buddypress, but i tried it using basic wordpress roles and it worked as well.

    so far so good, at least…


    Henry Wright
    Moderator

    @henrywright

    @somethingelse doing if ( $wp_user->roles[0] == 's2member_level3' ) inside the loop is great but you might find your pagination doesn’t take your condition into account. For that you’ll need to hook into the bp_ajax_querystring filter

    See the technique in action on the BuddyDev website:

    Exclude Users from Members directory on a BuddyPress based social network


    somethingelse
    Participant

    @somethingelse

    @Henry – thanks for that link…
    but it, again, seems to just exclude members by ID… our membership changes regularly… i don’t want to have to re-code my members-loop every time a member leaves or joins… we’re gonna be into id’s in the 1000’s soon!

    so… i backed up and tried @megainfo’s approach.
    that seems to EXCLUDE just fine… so far…
    not sure why i didn’t try that in the first place.

    now if i could just get rid of the darn “active users” issue…


    Henry Wright
    Moderator

    @henrywright

    @somethingelse great to see you got it working for you.

    our membership changes regularly… i don’t want to have to re-code my members-loop every time a member leaves or joins

    Why must the exclude ID list be fixed?

    I wouldn’t suggest doing something like $excluded_users = '1,2,3';

    Rather, your list of IDs to exclude should be dynamic e.g.

    $excluded_users = $ids;

    where $ids is a list of your users with role s2member_level3 – which is continually updated. To get $ids you’d just need to query for the s2member_level3 role.


    somethingelse
    Participant

    @somethingelse

    @henrywright – that’s a brilliant idea.
    i will try it… operative word being “try” … cuz i am far from being a programmer. i’m just a lucky guesser most of the time, and good at recognizing patterns.

    thanks for the tip!


    Marc
    Participant

    @marcangel

    Hi @megainfo I try your code and its working great on mine.
    But can we make it dynamic??
    Because I setup two members directory and I wanted to show, userrole1 to first directory page and userrole2 for the second page.
    Aprreciate help

Viewing 16 replies - 1 through 16 (of 16 total)
  • The topic ‘Excluding Roles from the Member's Directory.’ is closed to new replies.
Skip to toolbar