
The ajax filters that BuddyBoss supplies for the Members Directory – profile type and sort order – are both undependable to the point of dysfunction and difficult to modify to the point of dyspepsia at least.
All that we wanted was a simple selector allowing us to return members of particular LearnDash groups when viewing the directory. Using the bp_ajax_query_string filter turned out to be the key, but understanding its peculiar behavior and invisible background relationship (or disrelationship) with Profile Search also turned out to be critical.
A simpler objective was to limit the users shown in the directory to users of the current site (aka “blog”) and to override the “per_page” settings for pagination. Adding pagination links to the top of the directory required directing editing – via addition of a simple template tag – bp_nouveau_pagination( 'top' ) – to our already heavily customized template file buddypress/members/members-loop.php

/**
* In multisite installation, ensure BuddyPress Members Directory counts and displays users of the current site,
* aka "current blog" only
* @param string | array $ajax_query_string A component name or array of arguments modifying the BuddyPress user query
* @note Use a priority of 100 or higher in order to avoid conflicts with bps-search.php
*/
add_filter( 'bp_ajax_querystring', 'gbesu_modify_member_query_string_base', 100, 2 ) ;
function gbesu_modify_member_query_string_base( $ajax_query_string, $object ) {
if ( strpos( $ajax_query_string, 'include' ) ) {
return $ajax_query_string ;
}
if ( 'members' === $object ) {
$user_ids = get_users( array(
'fields' => 'ID',
) );
$ajax_query_string = add_query_arg( array(
'include' => $user_ids,
'per_page' => 50 ),
$ajax_query_string ) ;
return $ajax_query_string ;
}
return $ajax_query_string ;
}
/**
* Add Simple Group Selection Filter to Members Directory
* @not-todo writing as BuddyBoss or javascript filter too time-consuming for simple requirement
* @not-todo also for rewriting members count to refer only to local installation
*/
add_action( 'bp_before_members_loop', 'gbesu_add_members_group_filter' ) ;
function gbesu_add_members_group_filter() {
$group_ids = get_posts( array(
'numberposts' => -1,
'fields' => 'ids',
'post_type' => 'groups',
'orderby' => 'name',
'order' => 'ASC'
) ) ;
$referer = $_SERVER['HTTP_REFERER'] ; //find Group ID at end of url ("referer") because BuddyPress member filters bypass "$_GET"
if ( strpos( $referer, 'group_id' ) ) {
$selected = absint( substr( $referer, strpos( $referer, "=" ) + 1 ) ) ;
}
?>
<div id="member-group-filters" class="component-filters clearfix">
<div id="member-group__select" class="filter">
<form name="Filter" method="GET" action="" id="members-groups__form" class="members-groups__form">
<select id="member-groups" name="group_id" style="max-width: 200px">
<option value=""><?php _e( 'All Groups', 'buddyboss' ); ?></option><?php
foreach ( $group_ids as $group_id ) {
$attr = '' ;
$group_name = get_the_title( $group_id ) ;
if ( $selected === $group_id ) {
$attr = 'selected' ;
}
?>
<option class="member-group" value="<?php echo esc_attr( $group_id ); ?>" <?php echo $attr ; ?>>
<?php echo esc_attr( $group_name ); ?>
</option>
<?php
}
?>
</select>
<input id="member-group__submit" class="member-group__submit" type="submit" value=">">
</form>
</div>
</div>
<?php
//add priority 200 because bps-search.php member filter at 99 throws error due to include of an array
add_filter( 'bp_ajax_querystring', 'gbesu_modify_member_query_string', 200, 2 ) ;
}
function gbesu_modify_member_query_string( $ajax_query_string, $object ) {
if ( 'members' === $object ) {
$referer = $_SERVER['HTTP_REFERER'] ; //find Group ID at end of url ("referer") because BuddyPress member filters bypass "$_GET"
if ( strpos( $referer, 'group_id' ) ) {
$group_id = absint( substr( $referer, strpos( $referer, "=" ) + 1 ) ) ;
if ( $group_id ) {
$user_ids = learndash_get_groups_user_ids( $group_id ) ;
$ajax_query_string = add_query_arg( array(
'include' => $user_ids,
'per_page' => 50 ),
$ajax_query_string ) ;
return $ajax_query_string ;
}
}
remove_filter( 'bp_ajax_querystring', 'gbesu_modify_member_query_string' ) ; //allow normal filters and bps-search to function
}
return $ajax_query_string ;
}
add_action( 'wp_enqueue_scripts', 'gbesu_clear_bp_search' ) ;
function gbesu_clear_bp_search() {
wp_enqueue_script( 'clean-bp-search', get_stylesheet_directory_uri() . '/js/gbesu-clean-bp-search.js', array( 'jquery' ), '1.17' );
}
/**
* Override BP Search settings when selecting LD Group
*/
jQuery(document).ready(function($) {
var event = new Event( 'change' );
$( '.member-group' ).on( 'click', function() {
$( '#form_212152' ).find( ':input' ).each(
function () {
switch ( this.type ) {
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'email':
case 'date':
case 'url':
case 'search':
case 'textarea':
$( this ).val( '' );
break;
case 'checkbox':
case 'radio':
this.checked = false;
this.dispatchEvent( event );
break;
}
}
);
}) ;
// $.removeCookie( 'bp_ps_request', {path: '/'} );
}) ;
