Created
November 30, 2015 23:28
-
-
Save gabrielmerovingi/41983476c4308666a42f to your computer and use it in GitHub Desktop.
Final code for myCRED Tutorial - http://mycred.me/tutorial/buddypress-sort-members-by-current-balance/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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