Reply To: Sorting by last name goes wrong when having multiple words as last name
You don’t show how you call function format_last_name_first.
But this approach is probably better:
// members loop - sort by last name if type== alphabetical
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' );
As shown, it will do what your function is doing. You will have to change the ‘-1’ variable, probably to ‘1’. More info.