Skip to:
Content
Pages
Categories
Search
Top
Bottom

Last Name Sorting of Member Directory – Standard Method Not Working


  • warrencat
    Participant

    @warrencat

    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/

Viewing 6 replies - 1 through 6 (of 6 total)

  • shanebp
    Moderator

    @shanebp

    Just for reference… This works on a basic installation:

    // add order options to members loop
    function pp_member_order_options() {
    	?>
    	<option value="alphabetical-last"><?php _e( 'Alphabetical - Last Name', 'buddypress' ); ?></option>
    	<?php
    }
    add_action( 'bp_members_directory_order_options', 'pp_member_order_options' );
    
    function pp_members_loop_selection( $bp_user_query ) {
    
    	//  if Order By is set to alphabetical-last, sort by last name
    	if ( 'alphabetical-last' == $bp_user_query->query_vars['type'] ) {
    		$bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index(u.display_name, ' ', -1)";
    	   
    		// if they have variable last names like: Jones de Smith
    		//$bp_user_query->uid_clauses['orderby'] = "ORDER BY substring_index( substring_index(u.display_name, ' ', 2), ' ', -1 )";
    	}
    
    }
    add_action ( 'bp_pre_user_query', 'pp_members_loop_selection', 20 );

    The bp_pre_user_query hook is very powerful, but if anything else on your site is using it then the results can be confusing.


    warrencat
    Participant

    @warrencat

    Thank you for providing this optional approach, @shanebp. If it exists elsewhere on the forum, I hadn’t yet found it. I’ll test with this and report back.


    warrencat
    Participant

    @warrencat

    I’ve implemented and tested this method and it has finally succeeded in getting my member directory to sort and display by last name. I have a very basic implementation of BP and was confounded as to why the other methods that seemed to work for others would not work at all in my environment.

    Thank you again for providing this, @shanebp. It seems this is a more reliable method for achieving the desired results than the other method I was seeing more often while researching solutions.


    ajwright
    Participant

    @ajwright

    I am trying to get this to work but it gives an error on the } on the 6th line. If I leave it no users show up, if I remove it then nothing seems to have changed at all. I’m running WOffice Intranet theme. @shanebp any clue?


    ajwright
    Participant

    @ajwright

    @shanebp is there any information I can provide you with that may help solve my issue? If so just tell me what to copy and paste. I apologize I’m very new to BuddyPress and this loop system is a big learning curve for me. Any guidance at all is greatly appreciated.


    shanebp
    Moderator

    @shanebp

    What exactly is the error?

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.
Skip to toolbar