Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filter Member Loop to show only members of a particular group

  • Avatar of evanvolgas
    evanvolgas
    Participant

    @evanvolgas

    Hi. Is it possible to filter the primary member loop (eg the one that might normally be located at http://xyz.com/members) to show only members of a particular group?

    Here’s what I’ve tried….

    looking for documentation about this (in members-loop.php) and how to edit it… so far no luck
    <?php if ( bp_has_members( bp_ajax_querystring( ‘members’ ) ) ) :

    the approach described here: by Perywinkle a little over two years ago.

    and adding an “if statement” to <?php while ( bp_members() ) : bp_the_member(); in members-loop.php. The if statement I tried adding was
    <?php while ( bp_members() ) : bp_the_member();
    if( groups_is_user_member( bp_get_member_user_id() , 9) ) :

    So far… no luck. Have any of you managed to filter the main members loop to include only members of a particular group? If so, can you help point me in the right direction?

    Thanks in advance for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    get group member IDs and then pass the IDs as a parameter to the loop using &include=

    Avatar of evanvolgas
    evanvolgas
    Participant

    @evanvolgas

    That’s what made sense to me too but I’m not really clear on where to apply the filter… is the filter applying to

    <?php if ( bp_has_members( bp_ajax_querystring( ‘members’ ) ) )

    or is it applying to

    <?php while ( bp_members() ) : bp_the_member();

    (or somewhere else for that matter). I know it’s probably obvious. Just having a hard time figuring out where to apply the filter.

    Thanks for your help and speedy reply. I appreciate it.

    Avatar of shanebp
    shanebp
    Moderator

    @shanebp

    It’s not a filter, it’s an argument.

    Look at
    function bp_has_members in
    bp-members/bp-members-template.php

    You will see this argument
    'include' => false, // Pass a user_id or a list (comma-separated or array) of user_ids to only show these users

    So write some sql, something like :

    global $wpdb;
    $group_ids = $wpdb->get_col("SELECT user_id FROM $wpdb->bp_groups_members WHERE group_id = 9");
    $group_ids = implode(',', $group_ids);
    if ( bp_has_members( 'include=' . $group_ids ) )
    Avatar of evanvolgas
    evanvolgas
    Participant

    @evanvolgas

    Awesome. Apologies for mixing terminology. Thanks so much for sending this. Makes perfect sense.

    Avatar of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

    Don’t put $wpdb in your template file. create a function in bp-custom.php

    then in template file place function and pass group id

    bp_get_group_member_id(’9′);

    This way code can be used again on site if needed and not be deleted if you change themes

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

You must be logged in to reply to this topic.