Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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
}
/**
* 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';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.