phpwordpressbuddypress

BuddyPress/Wordpress List all groups


I've created a shortcode to list all groups. But it's not a loop, it only lists the first group created.

function list_groups_shortcode($group) {

    return "<div><h2 align='center'>Groups</h2><button type='button' class='groupButton'><a href='" . bp_get_group_permalink( $group ) . "'>" . bp_get_group_name( $group ) . "</a></div>";
}
add_shortcode('groups', 'list_groups_shortcode');

Any help how to list all buddypress groups in a button and link to that group?

I tried the above code. It only lists one group in a button, not the array of groups.


Solution

  • Use groups_get_groups. Example shortcode: [list_groups]

    function list_groups_shortcode() {
    
        $groups = groups_get_groups( array(
            'per_page'      => 99,
            'show_hidden'   => true,
        ) );
    
        $groups = $groups['groups'];
    
        $list = '<div><h2 align='center'>Groups</h2>';
        
        foreach ( $groups as $group ) {
        
            $list .= '<br>' . "<button type='button' class='groupButton'><a href='" . bp_get_group_permalink( $group ) . "'>" . $group->name . '</a>';
    
        }
    
        $list .= '</div>';
    
        return $list;
        
    }
    add_shortcode('list_groups', 'list_groups_shortcode');