Skip to:
Content
Pages
Categories
Search
Top
Bottom

Customising members-loop pagination


  • mantismamita
    Participant

    @mantismamita

    First of all I am using WordPress 4.1.1 and BuddyPress 2.2.2.1 and I am creating custom templates (via a custom plugin) that show Members based on roles. For example I want to have one page of only subscribers and another of only contributors. I have found a few different ways to doing this the simplest being to add a conditional statement like so

    $user = new WP_User( bp_get_member_user_id() );
    
                if ( $user->roles[0] =='subscriber'){ ?>

    within the bp members loop.

    The problem with this is that the pagination is skewed and is based on all the members instead of just those with the correct role. For example if I have 2000 members and only 5 of them are subscribers I will have pagination based on the 2000 where most pages are blank.

    I’ve read suggestions to modify the bp_members_pagination_count() but it seems that changing the parameters of bp_has_members() would be more efficient. Therefore I would need to modify bp_ajax_querystring( 'members' ) in ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) :

    I saw this thread by @zintax and modified it to suit my needs like so:

    function modify_members_loop( $qs=false, $object=false ) {
    
        if ( $object != 'members' ) // hide for members only
            return $qs;
    
        // create an array with my subscribed users
        $subscribers = get_users( 'role=subscriber' );
        $sub_list= array();
    
        foreach ( $subscribers as $sub ){
            $sub_list[]= $sub->ID;
        }
    
        $sub_list = implode(', ', $sub_list);
    
        if ( !empty( $args['include'] ) )
            $args['include'] = $args['include'] . ',' . $sub_list;
        else
            $args['include'] = $sub_list;
    
        $qs = build_query($args);
        return $qs;
    }
    
    add_action( 'bp_ajax_querystring' , 'modify_members_loop', 25, 2 );

    (I will later add conditionals so that this only occurs on the subscriber page)

    I was wondering if this is indeed the best and most query efficient way to go about this or if there are other considerations I should be mindful of.

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

  • shanebp
    Moderator

    @shanebp

    Your approach should be okay, but…
    Since you only want member IDs, then only request IDs.
    You can avoid retrieving user objects for each member and then having to extract their ID by doing this:

    function modify_members_loop( $qs=false, $object=false ) {
    
        if ( $object != 'members' )
            return $qs;
    
        $subscribers =  get_users( 'fields=ID&role=subscriber' );
        
        $args['include'] =  $subscribers;
    	
        $qs = build_query($args);
    
        return $qs;
    }
    add_action( 'bp_ajax_querystring' , 'modify_members_loop', 25, 2 );

    mantismamita
    Participant

    @mantismamita

    Ah great, I hadn’t thought of that. Thank you!

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