Created
December 27, 2022 00:59
-
-
Save KaineLabs/91da74bff7a85ed05a9446adee8a5545 to your computer and use it in GitHub Desktop.
Youzify - Sort Members Directory by Ratings
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
<?php | |
// Add a "By ratings" option to the sorting dropdown menu | |
function yzc_add_members_directory_ratings_filter() { | |
echo '<option value="youzify_user_ratings_total">' . __( 'By ratings', 'youzer' ) . '</option>'; | |
} | |
add_action( 'bp_members_directory_order_options', 'yzc_add_members_directory_ratings_filter' ); | |
// Modify the query to sort the members by the number of ratings in descending order | |
function yzc_sort_members_youzify_user_ratings_total( $query ) { | |
if ( $query->query_vars['type'] === 'youzify_user_ratings_total' ) { | |
global $wpdb; | |
$query->uid_clauses['select'] = "SELECT DISTINCT u.{$query->uid_name} as id FROM {$wpdb->users} u INNER JOIN {$wpdb->usermeta} um ON ( u.{$query->uid_name} = um.user_id )"; | |
// Adjust WHERE | |
$query->uid_clauses['where'] = "WHERE um.meta_key = 'youzify_user_reviews_count'"; | |
// Adjust ORDER BY | |
$query->uid_clauses['orderby'] = "ORDER BY um.meta_value+0"; | |
// Adjust ORDER | |
$query->uid_clauses['order'] = 'DESC'; | |
} | |
} | |
add_action( 'bp_pre_user_query', 'yzc_sort_members_youzify_user_ratings_total' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment