Customising the members loop and using this plugin

BP Profile Search Customising the members loop and using this plugin

  • Author
    Post
  • #3269 Reply
    Daniel
    Guest

    Hi

    It seems as though customising the members loop as recommended on the BuddyPress website seems to break this plugin as it will not filtered when the members loop is customised as shown:

    if ( bp_has_members(bp_ajax_querystring( ‘members’ ).’&type=alphabetical&include=’. include_experts() ) )

    #3272 Reply
    andrea
    Plugin Author

    Hi Daniel,

    Could you point me to the page where they offer that suggestion?

    In my opinion that’s not the correct way to customize the members loop when the include query arg is present, because it will certainly break all the plugins that use the same query arg, as BP Profile Search does.

    If you are interested I could post the correct way to achieve the same customization.

    #3274 Reply
    Daniel
    Guest

    Actually, I just noticed on this page (https://codex.buddypress.org/developer/loops-reference/the-members-loop/) they don’t have the bp_ajax_querystring function in the code examples. Only in the default setup.

    Yes please do show me as I think I have it set up wrong.

    #3275 Reply
    andrea
    Plugin Author

    You can add this code in your theme functions.php or in bp-custom.php:

    add_action ('bp_ajax_querystring', 'expert_inclusion', 10, 2);
    function expert_inclusion ($qs=false, $object=false)
    {
    	if ($object != 'members')  return $qs;
    
    	$args = wp_parse_args ($qs);
    	$users = explode (',', include_experts ());
    
    	if (isset ($args['include']))
    	{
    		$included = explode (',', $args['include']);
    		$users = array_intersect ($users, $included);
    		if (count ($users) == 0)  $users = array (0);
    	}
    
    	$args['include'] = implode (',', $users);
    	$qs = build_query ($args);
    
    	return $qs;
    }
    
    #3276 Reply
    Daniel
    Guest

    Hi Andrea

    Thanks for that code. That doesn’t seem to do anything though. It isn’t returning the ‘expert’ users, but everyone. I tried this in both functions.php and bp-custom.php.

    Is my members loop now supposed to be just

    if ( bp_has_members(bp_ajax_querystring( 'members' ) ) ) :

    ?

    Also, is it possible to set the ‘order by’ using

    $args[‘type’] = ‘alphabetical’;

    ?

    Is it possible to make this dynamic in case people change the order by type?

    #3277 Reply
    Daniel
    Guest

    This seems to work 🙂

    function expert_loop_querystring( $query_string, $object ) {
        if ( ! empty( $query_string ) ) {
            $query_string .= '&';
        }
     
        $query_string .= 'type=alphabetical&include='.include_experts();
     
        return $query_string;
    }
    add_filter( 'bp_ajax_querystring', 'expert_loop_querystring', 20, 2 );
    #3279 Reply
    andrea
    Plugin Author

    I’m glad to hear you made it work!

    The general idea of my code snippet was the same, but your implementation is simpler and better yet, it works! I only suggest you add this as the first line in your function:

    if ($object != 'members')  return $query_string;
    
    #3283 Reply
    Daniel
    Guest

    Is that to stop it filtering if it isn’t the members loop?

    #3287 Reply
    andrea
    Plugin Author

    Yes, exactly.

Reply to: Customising the members loop and using this plugin
My Information