Skip to:
Content
Pages
Categories
Search
Top
Bottom

Show members with certain role


  • mattvd
    Participant

    @mattvd

    Hi there. Is there a way to have TWO lists of all members on your site. One, the list of all members, but have another page filled with the members say who possess the editor role?

    I’ve seen topics about modifying the member loop to find members with certain roles, but by doing this, then it won’t show all members on your site. I would like to have two pages, one with all members, the other with only editors.

    Thanks for all your time!!

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

  • Henry
    Member

    @henrywright-1

    If you use this code directly below while ( bp_members() ) : bp_the_member(); in the members loop, you’ll be able to see which members are editors.

    <?php 
       $user_id = bp_get_member_user_id(); 
       $user = new WP_User( $user_id );
       
       if ( $user->roles[0] == 'editor' ) {
          echo 'this user is an editor';
       } else {
          echo 'this user is not an editor';
       }
    ?>

    I know this isn’t exactly what you’d like to do but it should put you on the right track.

    Note: This code assumes that all of your users have a single role assigned.


    mattvd
    Participant

    @mattvd

    Thanks for your help!

    So that will still output all members, but just add a note saying which ones are editors, correct? I would still like to have however a /members page that works right out of the box displaying all members. Just would like a separate page showing only editors.


    Henry
    Member

    @henrywright-1

    <?php 
       $user_id = bp_get_member_user_id(); 
       $user = new WP_User( $user_id );
       
       if ( $user->roles[0] == 'editor' )
       break;
    ?>

    If you add that code directly below while ( bp_members() ) : bp_the_member(); you’ll get a list of members who are not editors.


    Henry
    Member

    @henrywright-1

    <?php 
       $user_id = bp_get_member_user_id(); 
       $user = new WP_User( $user_id );
       
       if ( $user->roles[0] != 'editor' )
       break;
    ?>

    That’ll display only editors


    mattvd
    Participant

    @mattvd

    Thank you henry! Now how can I implement that into my site? Do I have to create a new php file add the code that will display editors to create a new page that outputs only editors?


    Henry
    Member

    @henrywright-1

    It is easily done. You’d need to create a new page template:

    https://codex.wordpress.org/Page_Templates

    You can then use a modified members loop inside the page template you create:

    Members Loop


    mattvd
    Participant

    @mattvd

    Hi henry thanks for all of your help.

    I think I have the code somewhat working, at least it says up top viewing members 1 of 5 (I have 1 member assigned as editor). But it doesn’t pull anything like their avatar and name etc.

    http://postimg.org/image/ay8fbd23f/

    Here is my code (the part that you recommended)

    <ul id="members-list" class="item-list" role="main">
     
        <?php while ( bp_members() ) : bp_the_member(); ?>
        
        <?php 
       $user_id = bp_get_member_user_id(); 
       $user = new WP_User( $user_id );
       
       if ( $user->roles[0] != 'editor' )
       break;
    ?>
     
            <li>
                <div class="item-avatar">
                    <a href="<?php bp_member_permalink(); ?>"><?php bp_member_avatar(); ?></a>
                </div>
     
                <div class="item">
                    <div class="item-title">
                        <a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a>
     
                        <?php if ( bp_get_member_latest_update() ) : ?>
     
                            <span class="update"> <?php bp_member_latest_update(); ?></span>
     
                        <?php endif; ?>
     
                    </div>
     
                    <div class="item-meta"><span class="activity"><?php bp_member_last_active(); ?></span></div>
     
                    <?php do_action( 'bp_directory_members_item' ); ?>
     
                    <?php
                     /***
                      * If you want to show specific profile fields here you can,
                      * but it'll add an extra query for each member in the loop
                      * (only one regardless of the number of fields you show):
                      *
                      * bp_member_profile_data( 'field=the field name' );
                      */
                    ?>
                </div>
     
                <div class="action">
     
                    <?php do_action( 'bp_directory_members_actions' ); ?>
     
                </div>
     
                <div class="clear"></div>
            </li>
     
        <?php endwhile; ?>

    shanebp
    Moderator

    @shanebp

    Change break; to continue;


    mattvd
    Participant

    @mattvd

    Brilliant! Thanks so much!


    mattvd
    Participant

    @mattvd

    Hi everyone,

    Thanks for all of your help! Now my next question is, is it possible to display all members with the role greater than editor? I’m not a php coder (as you can tell)

    But could we modify this snippet?
    if ( $user->roles[0] != 'editor' )
    To say greater than editor?


    vicksterm
    Participant

    @vicksterm

    This is very helpful, but the counts for active members on the Member Directory page are still incorrect. I copied the members-loop.php into a buddypress/members directory in my child theme and added the code where you described, but obviously the count for “All Members” and “Viewing x of x members” are coming from somewhere else.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Show members with certain role’ is closed to new replies.
Skip to toolbar