Buddypress how to sort Groups page alphabetically by default

In this short blog post I’ll run over how you can sort the Groups index page in Buddypress alphabetically by default (i.e. on first load of page they will be ordered by group name).

Edit or override 2 of the Buddypress templates

You’ll need these in your theme:

wp-content/YOUR_THEMENAME/buddypress/groups/index.php

and

wp-content/YOUR_THEMENAME/buddypress/groups/groups-loop.php

If these templates don’t exist in your theme you’ll need to make copies of the 2 templates from the Buddypress plugin  ( find in buddypress/bp-templates/bp-legacy/buddypress ) and pop into your theme directory in a folder called buddypress. You might need to copy from your parent theme if they are used there ( this is because of the buddypress / wordpress template hierachy – you can look that up if you don’t understand this).

 

Changes in groups-loop buddypress template

Before the bp_has_groups function call you need to add:

if ( bp_ajax_querystring( 'groups' ) ==""){  
    // default to ordering alphabetically 
    $queryString = "type=alphabetical&action=alphabetical&page=1";
    //$queryString ="";
}
else {
    $queryString = bp_ajax_querystring( 'groups' );    
}

and then change the bp_has_groups function to:

<?php if ( bp_has_groups(  $queryString  ) ) : ?>

 

Changes in index groups template

find this section and change to have alphabetical first ( you could also use the selected attribute ):

<select id="groups-order-by">
    <option value="alphabetical"><?php _e( 'Alphabetical', 'buddyboss' ); ?></option>
    <option value="newest"><?php _e( 'Newly Created', 'buddyboss' ); ?></option>
        <option value="active"><?php _e( 'Last Active', 'buddyboss' ); ?></option>
        <option value="popular"><?php _e( 'Most Members', 'buddyboss' ); ?></option>



        <?php do_action( 'bp_groups_directory_order_options' ); ?>
</select>

 

And thats it job done. Remember Buddypress can save your select choices in the session, which can be confusing – so clearing the cache will help in this scenario.

 

 

 

Leave a Comment