Last active
October 16, 2024 19:39
-
-
Save mgmartel/4463855 to your computer and use it in GitHub Desktop.
Sort BuddyPress users by last name in the members directory. Updated for BP 1.7.
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 | |
// Sort BuddyPress users by last name | |
// | |
// Use this Gist to sort members alphabetically by last name in the | |
// members directory. Follow the instructions below and drop the code | |
// in your functions.php or your functions plugin. | |
// | |
// Updated to work with BP 1.7. See earlier revisions of this gist for | |
// earlier versions of BP. | |
/** | |
* Sort users by last name | |
* | |
* Changes the querystring for the member directory to sort users by their last name | |
* | |
* @param BP_User_Query $bp_user_query | |
*/ | |
public function alphabetize_by_last_name( $bp_user_query ) { | |
if ( 'alphabetical' == $bp_user_query->query_vars['type'] ) | |
$bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.display_name, ' ', -1)"; | |
} | |
add_action ( 'bp_pre_user_query', 'alphabetize_by_last_name' ); | |
// To reflect the alphabetical sorting of the member directory, you | |
// could also format the names to show the last name first. Here is | |
// a helper function to format names as follows: Last, First Middle | |
// Etc. | |
// | |
// To use this function every time a user's name is shown or queried, | |
// simply add it as a filter: | |
// | |
// `add_filter ('bp_get_member_name', 'format_last_name_first' );` | |
// | |
// To only reformat the name in the member directory, change your | |
// members/members-loop.php file in your (child)theme. Look for | |
// `bp_member_name()` | |
// and replace it with | |
// `echo format_last_name_first ( bp_get_member_name() );` | |
/** | |
* Helper function for formatting a name: Last, First Middle Etc. | |
* | |
* @param string $name | |
* @return string Formatted name | |
*/ | |
function format_last_name_first( $name ) { | |
if ( $first_space = strrpos( $name, " ") ) | |
$name = substr( $name, $first_space + 1 ) . ", " . substr( $name, 0, $first_space ); | |
return $name; | |
} | |
// BONUS: To make alphabetical sorting the default sorting, use the | |
// function below. | |
// | |
// In order for this to work properly, `<option value="alphabetical">` | |
// must be the first option for the members-order-select in | |
// members/index.php. | |
function sort_members_alphabetically( $qs = '', $object = false ) { | |
if( $object != 'members' ) //for members only | |
return $qs; | |
if ( empty( $qs ) || ! strpos( $qs, "type=" ) ) { | |
$args = wp_parse_args($qs); | |
$args['type'] = 'alphabetical'; | |
$args['action'] = 'alphabetical'; | |
$qs = build_query( $args ); | |
} | |
return $qs; | |
} | |
add_action( 'bp_ajax_querystring', 'sort_members_alphabetically', 11, 2 ); |
How sort members by custom profile field?
function sort_members_alphabetically( $qs = '', $object = false ) {
if( $object != 'members' ) //for members only
return $qs;
if ( empty( $qs ) || ! strpos( $qs, "type=" ) ) {
$args = wp_parse_args($qs);
$args['type'] = 'alphabetical';
$args['action'] = 'alphabetical';
$qs = build_query( $args );
}
return $qs;
}
add_action( 'bp_ajax_querystring', 'sort_members_alphabetically', 11, 2 );
Replace
if ( empty( $qs ) || ! strpos( $qs, "type=" ) ) {
with:
if ( empty( $qs ) || (strpos( $qs, "type=" ) === false) {
@[mgmartel]
Today 11th of December 2023, I am using BuddyPress 11.4.0
And Yes, I have struggled on how to Sort BuddyPress Members in Alphabetical Order. I think I've still not found the solution.
Can You please extend this Code to be 2 in 1?
By this, I mean that it should enable us to sort by:
a.) Alphabetical Order
b.) First Name and Last Name.
Can You Please extend this code to be able to do this?
And can you see to it, that the code will be compatible with BuddyPress 11.4.0 ?
Regards.
for me the first solution was not working, cause many people hat last names with multiple words like "von Albe".
for me this worked (like this i was able to sort by a specific profile field.):
function alphabetize_by_last_name($bp_user_query)
{
global $wpdb;
$table_prefix = $wpdb->prefix;
$prfile_field_name = "Familienname";
if ('alphabetical' == $bp_user_query->query_vars['type']) {
$query = $bp_user_query->uid_clauses['select'];
$query = $query . " left join (select pd.user_id, pd.value as field_value
from {$table_prefix}bp_xprofile_data pd
left join {$table_prefix}bp_xprofile_fields pf on pf.id = pd.field_id
where pf.name = '{$prfile_field_name}') pln on pln.user_id = u.ID";
$bp_user_query->uid_clauses['orderby'] = "ORDER by IFNULL(field_value, u.display_name)";
$bp_user_query->uid_clauses['select'] = $query;
}
}
add_action('bp_pre_user_query', 'alphabetize_by_last_name');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, I am also stuck. I put this code in the functions.php file of my child theme and no luck.
Any help would be appreciated!