Skip to:
Content
Pages
Categories
Search
Top
Bottom

Reply To: Adding new ‘Order By’ on members loop

@shanebp

Moderator

You don’t need to touch any core files to add an Order By.

Example for adding a Contributor option, put in bp-custom.php or theme/functions.php

// add order options to members loop
function ch_member_order_options() {
?>
   <option value="contributing"><?php _e( 'Contributing Members', 'buddypress' ); ?></option>
<?php
}
add_action( 'bp_members_directory_order_options', 'ch_member_order_options' );

// filter ajax members by contributing
function ch_filter_ajax_querystring( $querystring = '', $object = '' ) {

	if( $object != 'members' )
		return $querystring;

	$defaults = array(
		'type'            => 'active',
		'action'          => 'active',
		'scope'           => 'all',
		'page'            => 1,
		'user_id'         => 0,
		'search_terms'    => '',
		'exclude'         => false,
	);

	$ch_querystring = wp_parse_args( $querystring, $defaults );

	if( $ch_querystring['type'] == 'contributing' ) {
	
// to get members by xprofile field, you need some custom sql here
// here's an example: 
//https://codex.buddypress.org/developer/loops-reference/the-members-loop/#code-examples

		$users = get_users( array( 'fields' => array( 'ID' ), 'role' => 'contributor' ) );
		
		$users_str = '';
		foreach ( $users as $user ) {
	             $users_str .=  $user->ID . ',';
	        }
		$users_str = rtrim($users_str, ",");

		$ch_querystring['include'] = $users_str; 
		$ch_querystring['type'] = 'alphabetical'; 
		
		return $ch_querystring;
		
	}	
	
	else
		return $querystring;
			
}
add_filter( 'bp_ajax_querystring', 'ch_filter_ajax_querystring', 20, 2 );
Skip to toolbar