Last Name Sorting of Member Directory – Standard Method Not Working
-
I’ve been trying for a while with no success to order the member directory by last name (surname) instead of first name. Extensive searching through the support forum and elsewhere would indicate that the following code modifications to a custom members-loop.php file in the theme folder and the theme functions.php file should accomplish this.
My custom members-loop.php file contains:
<?php if ( bp_has_members( bp_ajax_querystring( 'members' ) . '&populate_extras&type=alphabetical' . '&per_page=30' ) ) : ?>
My theme functions.php file contains:
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' );
Instead of sorting and displaying members by last name, it returns the message:
Sorry, no members were found.
If I change ‘u.display_name’ in the functions.php file code to ‘u.value’, all the members are listed in the default alphabetical sort order by first name. So, it appears that, for some reason, the code won’t output the expected results when ‘u.display_name’ is used.
I’m posting here in the hopes that someone in this support community might have some additional guidance or suggestions that could help me figure this out.
Here are a couple of the links I’ve previously referenced when trying to get this working:
https://buddypress.org/support/topic/sort-user-list-by-last-name/
https://buddypress.org/support/topic/sorting-by-last-name-goes-wrong-when-having-multiple-words-as-last-name/
- You must be logged in to reply to this topic.