Skip to:
Content
Pages
Categories
Search
Top
Bottom

Sort Members Alphabetically by Default


  • VentureCore
    Participant

    @manakio2k

    I have read a lot of threads here regarding the default displays for members alphabetically and at one point I had found a solution that worked but it was recently overwritten by an upgrade. (I failed to put it in the child theme) – my bad!

    So I went on the search again and found this. (I don’t think it’s what I was using)

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

    When I place this code in my members-loop it works but then I can’t use the drop-down search to return users by “Last Active” or “Newest Registered”; it just keeps returning by alphabetical only.

    Any help would be appreciated!

    Thanks…

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

  • danbp
    Moderator

    @danbp

    Hi @manakio2k,

    you could try to just modify the template to get alphabetical as first sort option, instead of the default Last active.

    Create a child-theme first, add a folder called buddypress. In this folder add members folder and in that folder add a copy of index.php which is in bp-templates/bp-legacy/buddypress/members/index.php

    Around line 78, change the options order to get this:

    <select id="members-order-by">
    	<?php if ( bp_is_active( 'xprofile' ) ) : ?>
    		<option value="alphabetical"><?php _e( 'Alphabetical', 'buddypress' ); ?></option>
    	<?php endif; ?>
    		<option value="active"><?php _e( 'Last Active', 'buddypress' ); ?></option>
    		<option value="newest"><?php _e( 'Newest Registered', 'buddypress' ); ?></option>

    Now, members directory is sorted alphabetically by default.


    VentureCore
    Participant

    @manakio2k

    Thank you,

    I tried that both in members-loop and now in index and neither work. It changes the order the list shows up in the dropdown but does not affect the default sort order.

    I had a solution but didn’t record it, it is either not working now or got overwritten through an update.

    Any other thoughts? 🙂


    VentureCore
    Participant

    @manakio2k

    OK, so here’s the solution…

    <?php /* Querystring is set via AJAX in _inc/ajax.php - bp_dtheme_object_filter() */ ?>
    <?php do_action( 'bp_before_members_loop' ) ?>
    
    <?php
    if ( bp_ajax_querystring( 'members' ) ==""){
    $queryString = "type=alphabetical&action=alphabetical&page=1";}
    else {$queryString = bp_ajax_querystring( 'members' );}
    ?>
    
    <?php if ( bp_has_members( $queryString) ) : ?>

    Depending on how/if your theme developer has written their members-loop.php you will want to replace these two entries…

    <?php do_action( 'bp_before_members_loop' ); ?>
    
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>

    This not only gives you the ability to have buddypress default to listing names Alphabetically, it also maintains your ability to sort ‘Last Active’ and ‘Newest Registered’. This should be the default way to display the members directory. IMOP

    Now, if you want to then change the alphabetical listing default to return names by the lastname of your members you would add the following code to your theme’s function.php file. Preferably to the child theme’s function.php file.

    /** Sort alphabetical name listings by lastname */
    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' );

    Good luck!


    VentureCore
    Participant

    @manakio2k

    Question:

    This works with and without the semicolon after string in ‘before_members_loop’

    <?php do_action( 'bp_before_members_loop' ); ?>
    or
    <?php do_action( 'bp_before_members_loop' ) ?>

    Do you see either of these causing other problems or preventing other calls?


    kmw1130
    Participant

    @kmw1130

    I tried the solution mentioned and still not sorting alphabetically, the dropdown defaults to Alphabetical, but doesn’t sort. Is it necessary to add the code to the function.php file, I’d like to see it sort alphabetically by firstname to know that it is working.


    VentureCore
    Participant

    @manakio2k

    You can see it working here… http://qr2.bz/3days
    Link will expire in 3days 🙂


    kmw1130
    Participant

    @kmw1130

    And the change is added to the members-loop.php page, correct? also changed the order in the index.php file, neither seem to make a difference for me. Maybe I’m missing something.

    Thanks
    Kim


    VentureCore
    Participant

    @manakio2k

    You have to add part of the code to the members loop and part of the code goes in your functions.php file. You also have to manually reorder the php to display/default alphabetical first in the dropdown menu


    kmw1130
    Participant

    @kmw1130

    I added this to members-loop:

    <?php /* Querystring is set via AJAX in _inc/ajax.php - bp_dtheme_object_filter() */ ?>
    <?php do_action( 'bp_before_members_loop' ) ?>
    
    <?php
    if ( bp_ajax_querystring( 'members' ) ==""){
    $queryString = "type=alphabetical&action=alphabetical&page=1";}
    else {$queryString = bp_ajax_querystring( 'members' );}
    ?>
    
    <?php if ( bp_has_members( $queryString) ) : ?>
    
    <strong>Index.php</strong>
    
    <select id="members-order-by">
    	<?php if ( bp_is_active( 'xprofile' ) ) : ?>
    		<option value="alphabetical"><?php _e( 'Alphabetical', 'buddypress' ); ?></option>
    	<?php endif; ?>
    		<option value="active"><?php _e( 'Last Active', 'buddypress' ); ?></option>
    		<option value="newest"><?php _e( 'Newest Registered', 'buddypress' 
    
    Functions.php (child theme)
    /** Sort alphabetical name listings by lastname */
    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' );

    Is that correct?

    Thanks
    Kim


    kmw1130
    Participant

    @kmw1130

    It seems like any changes I make to the members-loop.php file doesn’t make a difference, so it makes me wonder if I am updating the correct file. (wp-content\child-theme\buddypress\members\members-loop.php) is the members-loop file I’ve updated. As well as my child-theme function and child-theme\buddypress\members\index.php.


    zigsolutions
    Participant

    @zigsolutions

    @kmw1130 … I’m having the same issue. Just trying to comment out the activity of the users that show up in the list for a specific group. Any changes I make to members-loop.php are not showing up. Not sure if I’m updating the correct file myself to make this change.

    Does anyone know how or where to make changes to the members-loop.php file so they show up in the latest version of BuddyPress?

    Thank you so much!


    Jon Crowell
    Participant

    @jcrowell

    @manakio2k, Thanks! it worked like a charm for me.

    For anyone still having issues, after
    <?php do_action( ‘bp_before_members_loop’ ) ?>
    Insert

    <?php
    if ( bp_ajax_querystring( 'members' ) =="") {
    	$queryString = "type=alphabetical&action=alphabetical&page=1";
    } else {
    	$queryString = bp_ajax_querystring( 'members' );
    }
    ?>

    Then replace
    <?php if ( bp_has_members( bp_ajax_querystring( 'members' ) ) ) : ?>
    with
    <?php if ( bp_has_members( $queryString) ) : ?>

    That’s it. If you want to limit the Ordering options, go into index.php and comment out the options in the dropdown.


    Jon Crowell
    Participant

    @jcrowell

    So I can’t get the sorting by LAST NAME to work. By default buddypress uses the display name on the members directory so I’m using this function to get it to display the xprofile data.

    function my_directory() {	
    if ( bp_is_active( 'xprofile' ) )
    
    	if ( $membername = xprofile_get_field_data( 'First Name', bp_get_member_user_id() ) . ' ' . xprofile_get_field_data( 'Last Name', bp_get_member_user_id() ) . ' ' . xprofile_get_field_data( 'Title', bp_get_member_user_id() ) ) :
    		echo '<div class="dir_name">';		
    		echo $membername;
    		echo '</div>';
    	endif;	
    
    }
    add_filter ( 'bp_member_name', 'my_directory' ); 

    But the function you provided above doesn’t sort by the xprofile data. Any help?


    livingflame
    Participant

    @livingflame

    @danbp Please, help me with this.

    Look at the picture. —> https://goo.gl/photos/r9g6fEqwr7dgCiXj7

    From Here, I want to remove Order By: And Put Newest Members or Newest Registered next to All Members and Friends.

    I saw Members / Index.php, but I dont know how to Get Bp–Total–Newest…List You know the Code.


    shaundot
    Participant

    @shaundot

    Hi just wondering what the solution would be for the bp-noveau theme? i dont see the same code in members-loop.php or index.php. thanks in advance!


    chaddblanchard
    Participant

    @chaddblanchard

    Anyone been able to get the sort to work in the new BP Nouveau?

    This does not exist in the members-loop.php of BP Nouveau:

    <?php do_action( ‘bp_before_members_loop’ ) ?>


    jadedartist
    Participant

    @jadedartist

    Also using BP Nouveau, and don’t find within members-loop.php:

    <?php do_action( ‘bp_before_members_loop’ ); ?>

    Any solution for Nouveau?


    jasonsubers
    Participant

    @jasonsubers

    You can use bp_parse_args() to do that.

    So for the members loop, the filters are

    bp_before_has_members_parse_args
    bp_after_has_members_parse_args

    I needed to only show members of a certain type in my directory, so this worked for me:

    function only_members( $retval ) {
    	$retval['member_type'] = array( 'type1', 'type2' );
    	return $retval;
    }
    add_filter( 'bp_before_has_members_parse_args', 'only_members' );

    jarkin13
    Participant

    @jarkin13

    To order alphabetically user the bp_nouveau_get_members_filters.

    function themename_bp_reorder_member_directory_filters( $filters, $context ) {
    	if ( 'group' !== $context ) {
    
    		$filters = array();
    
    		if ( bp_is_active( 'xprofile' ) ) {
    			$filters['alphabetical'] = __( 'Alphabetical', 'buddypress' );
    		}
    
    		$filters['active'] = __( 'Last Active', 'buddypress' );
    		$filters['newest'] = __( 'Newest Registered', 'buddypress' );
    	}
    
    	return $filters;
    }
    add_filter( 'bp_nouveau_get_members_filters', 'themename_bp_reorder_member_directory_filters', 10, 2 );

    ricpdragon
    Participant

    @ricpdragon

    Thanks all! And to order by “newest registered”?


    ebouvet
    Participant

    @ebouvet

    Merci beaucoup !


    saranghills
    Participant

    @saranghills

    Thank you very much! This worked for me!


    deswegen
    Participant

    @deswegen

    Where do I place this? Placed it in my functions.php but that did not do it. Still trying to sort alphabetically with Nouveau but can’t make it work :-/


    wackao
    Participant

    @wackao

    this is available by default in our theme WPLMS. Should be a control and users should not be required to add code for such simple things

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