Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Created November 30, 2015 23:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielmerovingi/41983476c4308666a42f to your computer and use it in GitHub Desktop.
Save gabrielmerovingi/41983476c4308666a42f to your computer and use it in GitHub Desktop.
/**
* 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