Skip to:
Content
Pages
Categories
Search
Top
Bottom

[Resolved] Sort user list by last name

  • @sallymander

    Participant

    I have searched the forum for an answer and can’t find what I need

    I need our member list to sort by Last Name instead of username. I have very limited (VERY) experience with making code changes, but I’m not afraid to try.

    Running WordPress 4.0
    BuddyPress 2.1

    site is http://www.homepatriots.com

    Thanks for any help!

Viewing 10 replies - 1 through 10 (of 10 total)
  • @danbp

    Participant

    hi @sallymander,

    use a child theme to do this.
    Copy members-loop.php file into /your-child-them/buddypress/members/ folder.

    Replace line 16
    if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) :

    by

    if ( bp_has_members( bp_ajax_querystring( 'members' ) . '&populate_extras&type=alphabetical' ) ) :

    Added the answer for the upcoming question: i want the list in alphabetical order. 🙂

    Reference

    Members Loop

    @sallymander

    Participant

    and this will use the Last Name field to alphabetize?

    I have never created a child theme for anything before, so this is a learning experience for me!
    THank you!

    @shanebp

    Moderator

    No, it will use the first word in the display name.

    To use the last word, follow danbp’s instructions and then paste this function in your child-theme/functions.php

    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' );

    @sallymander

    Participant

    Okay, I successfully created the child theme, but when I added the above code, I got error messages.
    I have this code in my members-loop.php file:
    <?php if ( bp_has_members( bp_ajax_querystring( ‘members’ ).’&type=alphabetical’ ) ) : ?>
    And it is successfully sorting by user name.

    What am I missing that it isn’t working?

    @danbp

    Participant

    Check your child-theme functions.php file.

    At the begin of the file you should have an opening php tag <?php
    on the third line you add Shane’s function.

    The file must be used without ending php tag ?> So if you added one after the function, remove it.

    other possibility:
    you entered the snippet with help of a text editor like Word or Sublime or…
    Bad idea ! Use a notepad editor or any other raw text editor. And at least, be always aware that php uses mostly straight cotes.

    In your case, until you confirm, it was the quotes !

    Here is your code, with the right quotes in place:
    if ( bp_has_members( bp_ajax_querystring( 'members' ) . '&type=alphabetical' ) ) :

    To write code with a text editor, you have to encode and/or save it in UTF-8 without BOM format.

    And the best for the end:
    when you publish code on this ofrum don’t use the php tags, and in any case, use the code button so it can be viewed in a proper format and won't mess up the whole forum.

    Thank you !

    @sallymander

    Participant

    I used the code editor in my cpanel. It shows the first user but under that is this error message: Fatal error: Call to undefined function bp_member_alphabetical() in …..wp-content/themes/twentyfourteen-child/buddypress/members/members-loop.php on line 57

    Here’s line 57-

    bp_member_alphabetical(); ?><

    Did I list that correctly this time? Thanks for your patience. This is a steep learning curve for me.

    @danbp

    Participant

    Seems you and me don’t use the same file !
    The code to change is line 16 at the begin of that file. This place didn’t change since my first answer… 😉

    The members-loop.php file in your child theme must be a copy of the one in
    buddypress/bp-templates/bp-legacy/buddypress/members/members-loop.php

    The modified line included in php tags looks like this once you finished correct copy/pasting
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) . '&type=alphabetical' ) ) : ?>

    @sallymander

    Participant

    Okay – so I have this
    <?php if ( bp_has_members( bp_ajax_querystring( ‘members’ ).’&type=alphabetical’ ) ) : ?>

    in the members-loop.php file and it sorts the users alphabetically by user name.

    BUT if I put this:

    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' );

    into the functions.php file I get this error message after it returns one user:

    Fatal error: Call to undefined function bp_member_alphabetical() in /home/homepatr/public_html/wp-content/themes/twentyfourteen-child/buddypress/members/members-loop.php on line 57

    And here is a copy of line 57:
    <div class="item-meta"><span class="activity"><?php bp_member_alphabetical(); ?></span></div>

    I’m just a homeschool mom/volunteer trying to get our support group web site up the way we need it. This is definitely a learning experience for me, and I appreciate all the help!

    @sallymander

    Participant

    Okay – I went and replaced the members-loop.php with a fresh copy, made the changes again and now it works.

    Who knows. Thanks much!

    @danbp

    Participant

    Bravo mom ! 😉 Glad you got it to work…

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Resolved] Sort user list by last name’ is closed to new replies.
Skip to toolbar