phpwordpressdirectorybuddypress

Buddypress: Default sort oder for Group Directory


I want the groups in the group-directory to be sorted alphabetically in Buddypress, unless the user picks his own sorting via dropdown.

I tried via custom functions:

add_filter('bp_after_has_groups_parse_args', function ($options) 
{
    if (bp_is_groups_directory()) {
        if(!$options['type']) 
            $options['type'] = 'alphabetical';
        
    }
    return $options;
});

As per documentation, $options[‘type’] is “null” when the user has NOT yet picked an option in the dropdown, i.e. when it’s the initial sorting.

At least it says it in line 995: https://github.com/buddypress/BuddyPress/blob/master/src/bp-groups/classes/class-bp-groups-group.php#L995

But it does not seem to work. Any ideas?


Solution

  • In buddy press 10.0.0, you just need to change the bp-groups-template.php in the following location:

    /wp-content/plugins/buddypress/bp-groups/bp-groups-template.php

    In the file, find this area:

    /**

    In the function bp_has_groups, find this section:

    $r = bp_parse_args(
        $args,
        array(
            'type'               => $type,
            'order'              => 'ASC',   // changed from DESC 
            'orderby'            => 'name',  // changed from last_activity 
    

    I added the info after the // so I know what changes were made from the original.

    This sets the default first viewing of the Groups page to an Alphabetical listing. We are implementing Youzify over top of BuddyPress, so I also changed the default Order By listing in

    /wp-content/plugins/youzify/includes/public/templates/groups/index.php

    /** * Fires inside the groups directory group types. * * @since 1.2.0 */ do_action( 'bp_groups_directory_group_types' ); ?>

                <li id="groups-order-select" class="last filter">
    
                    <label for="groups-order-by"><?php _e( 'Order By:', 'youzify' ); ?></label>
    
                    <select id="groups-order-by">
                        <option value="alphabetical"><?php _e( 'Alphabetical', 'youzify' ); ?></option>
                        <option value="active"><?php _e( 'Last Active', 'youzify' ); ?></option>
                        <option value="popular"><?php _e( 'Most Members', 'youzify' ); ?></option>
                        <option value="newest"><?php _e( 'Newly Created', 'youzify' ); ?></option>
    
                        <?php
    
                        /**
                         * Fires inside the groups directory group order options.
                         *
                         * @since 1.2.0
                         */
                        do_action( 'bp_groups_directory_order_options' ); ?>
                    </select>
    

    Only change here from the original was to take this line:

                        <option value="alphabetical"><?php _e( 'Alphabetical', 'youzify' ); ?></option>
    

    from the bottom of the list, to the top.

    Hope this helps anyone down the road.