Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filter Member Loop to show only members of a particular group

  • Profile photo 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 7 replies - 1 through 7 (of 7 total)
  • Profile photo of @modemlooper
    @modemlooper
    Moderator

    @modemlooper

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

    Profile photo 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.

    Profile photo 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 ) )
    Profile photo of evanvolgas
    evanvolgas
    Participant

    @evanvolgas

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

    Profile photo 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

    Profile photo of nick32601
    nick32601
    Participant

    @nick32601

    can someone please walk me through this step by step with code and location? not that great at php.

    I’m after adding group filters to the Order by: list on members-loop page

    thank you so much

    Profile photo of danbp
    danbp
    Moderator

    @danbp

    Things have changed since 10 mounth !
    You can do this by reading this tutorial:
    https://codex.buddypress.org/developer/using-bp_parse_args-to-filter-buddypress-template-loops/

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Filter Member Loop to show only members of a particular group’ is closed to new replies.
Skip to toolbar