Grab 75% Off Lifetime Access – Limited Time, Don't Miss Out!Get it NoW

BuddyPress: Sort members by current balance

Navigation:

Description

In this tutorial I will show you how you can sort your BuddyPress members list according to your users current points balances.

All code snippets in this tutorial goes into your themes functions.php file.

Requirements

  • BuddyPress 1.5+
  • myCred Installed and running

The setup

In order for us to sort users according to their balance, we will need to do two things:

  1. Insert the sorting order options into the drop-down menu on our members page.
  2. Adjust the BuddyPress user query to reflect our selected sorting option.

Adjusting the sorting drop-down menu

In the default BuddyPress theme, the drop-down menu that handles sorting on your members page is located in members/index.php and can be extended via the bp_members_directory_order_options action. So the first thing we are going to do is to hook into this action and insert our custom sorting options:

/**
 * Add Sort Members Option
 * @since 0.1
 * @version 1.0
 */
add_action( 'bp_members_directory_order_options', 'mycred_pro_add_sorting_options' );
function mycred_pro_add_sorting_options() { ?>
<option value="points-asc">Points Balance (Ascending)</option>
<option value="points-desc">Points Balance (Descending)</option>
<?php
}

The above example will insert two new options allowing us to sort users according to their current balance either in an Ascending order (0,1,2,3,4) or Descending order (10,9,8,7).

Adjust the BuddyPress User Query

Now that we have our custom options, we need to hook into BuddyPress and adjust the user query to reflect our selected options. To do this, we will use the bp_pre_user_query action which allows us to adjust the database query just before it is executed.

Incorrect SQL queries here will result in either an error message (if WP_DEBUG is enabled) or will return “No users found”.

/**
 * Adjust BP User Query
 * @since 0.1
 * @version 1.0
 */
add_action( 'bp_pre_user_query', 'mycred_pro_pre_user_query' );
function mycred_pro_pre_user_query( $BP_User_Query ) {
	// Only run this if one of our custom options is selected
	if ( in_array( $BP_User_Query->query_vars['type'], array( 'points-asc', 'points-desc' ) ) ) {
		global $wpdb;

		// Adjust SELECT
		$BP_User_Query->uid_clauses['select'] = "
SELECT DISTINCT u.{$BP_User_Query->uid_name} as id 
FROM {$wpdb->users} u 
INNER JOIN {$wpdb->usermeta} um 
	ON ( u.{$BP_User_Query->uid_name} = um.user_id )";

		// Adjust WHERE
		$BP_User_Query->uid_clauses['where'] = "WHERE um.meta_key = 'mycred_default'";

		// Adjust ORDER BY
		$BP_User_Query->uid_clauses['orderby'] = "ORDER BY um.meta_value+0";

		// Adjust ORDER
		$BP_User_Query->uid_clauses['order'] = ( $BP_User_Query->query_vars['type'] == 'points-asc' ) ? 'ASC' : 'DESC';
	}
}

For more information on how BuddyPress handles user searches, you can always check the bp-core-classes.php file which is located in plugins/buddypress/.

Done

That’s it! Save the above codes into your themes functions.php file and upload. If you have any questions or issues, feel free to post them below in the comments section.

11