Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filter members loop


  • barodscheff
    Participant

    @barodscheff

    Hi!

    I would like to filter the members loop after clicking on a link. The filtering itself isn’t the problem because its well documented (https://codex.buddypress.org/developer/loops-reference/the-members-loop/). I just don’t know how to apply the filter and where to write the code.

    For instance my members have the xprofile fields gender and pet. On my homepage I have the following:
    <a href="#">All male members who have poodles</a>
    After clicking on this a new page should be opened which displays all male members with poodles.

    My questions:
    1. How can I tell the new page what should be filtered?
    2. How can I filter for more than one field. In the doc there’s this
    <?php if ( bp_has_members( my_custom_ids( $field_name, $field_value ) ) ) : ?>
    Can I enter more than one $field_name and $field_value (male and poodle)?
    3. Which template do I have to use for the custom members loop?

    Thanks in advance!

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

  • Henry Wright
    Moderator

    @henrywright

    Hi @barodscheff

    You can just copy the code from here and paste it in to your page template (say page-poodles.php). You can then go about filtering the returned members as you see fit.


    rosyteddy
    Participant

    @rosyteddy

    Hi @henrywright

    Thats nice, very useful.
    Can you kindly say the exact file name where such code can be put? It is not clear to me where I put this code.
    Can you please, please say how can I have a Member list page ( or any other page) that lists the Members who are not the logged-in user’s friends?
    This means that a logged in user can see only names with “Add friend” buttons by the side of the names.

    Thanks


    Henry Wright
    Moderator

    @henrywright

    Hi @rosyteddy

    I haven’t tried it, but I think it’ll work if you put it in a page template. See here for more info.

    Regarding the functionality you want, you’d need to do something like this:

    $ids = '1, 2, 5, 33, 43';

    if ( bp_has_members( 'exclude=' . $ids ) ) :

    Where $ids is a comma separated list of user IDs who are not friends.


    rosyteddy
    Participant

    @rosyteddy

    Hi @henrywright

    Can you make a plugin out of it 🙂
    The main problem is how do I get the $ids array or whatever.

    This code by Brajesh does that

    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='24,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; 
    }

    Now all that I want is to $excluded_user = logged in user’s friends. How to do that ?

    Thanks


    Henry Wright
    Moderator

    @henrywright

    Brajesh seems to have done most of the heavy lifting for what you want. He uses the bp_ajax_querystring filter, so lets borrow his idea and make our own function to exclude friends from the loop.

    function rosyteddy_exclude_users( $querystring = false, $object = false ) {
    
        // Only do this for the members loop.
        if ( $object != 'members' )
            return $querystring;
    
        // Get an array of friend IDs for the logged-in user.
        $ids = friends_get_friend_user_ids( get_current_user_id() );
    
        // Make the array a comma sep list.
        $ids = implode( ',', $ids );
    
        $args = wp_parse_args( $querystring );
    
        if ( ! empty( $args['exclude'] ) )
            $args['exclude'] = $args['exclude'] . ',' . $ids;
        else
            $args['exclude'] = $ids;
    
        $querystring = build_query( $args );
    
        return $querystring;
    }
    add_action( 'bp_ajax_querystring', 'rosyteddy_exclude_users', 20, 2 );

    Note: I haven’t tested.


    rosyteddy
    Participant

    @rosyteddy

    Wow @henrywright you rock!

    Cool! My quick test shows its work perfectly.
    How to say thank you.

    Many, many thanks.


    Henry Wright
    Moderator

    @henrywright

    @rosyteddy glad to hear it works! 😀


    rosyteddy
    Participant

    @rosyteddy

    So, this was the plugin idea I posted sometime ago.
    In the above, now if you click “Add friend” some ajax trick should show Friendship Request sent
    and the row should fade out, and lower names should come up 🙂

    I do not know ajax tricks and am a bad learner 🙂

    Thanks a lot once again @henrywright for this great, great snippet.


    rosyteddy
    Participant

    @rosyteddy

    Additional idea to this : Exclude “Cancel Friendship Request” from this list if its not ajaxified like above to exlude those to whom request has been sent 🙂

    Sorry for the nagging 🙂

    Thanks always


    barodscheff
    Participant

    @barodscheff

    Thank you @henrywright. Maybe my question wasn’t clear enough.

    I’ve already copied the code in a custom template. But how do I call the template? What should be the link target? What do I have to write instead of # to call the template with the filtering (e.g. male members with poodles)
    <a href="#">All male members who have poodles</a>


    barodscheff
    Participant

    @barodscheff

    Ok, I solved it with page templates.
    Thank you for your help!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Filter members loop’ is closed to new replies.
Skip to toolbar