Skip to:
Content
Pages
Categories
Search
Top
Bottom

Filter Hidden Groups from the Groups Loop

  • @garrett-eclipse

    Participant

    Hello,

    Wanted to share my findings are they were a pain to find…
    If you want to filter hidden groups from the Groups Loop you’ll have to add a filter hooking onto the groups_sql calls:
    // Hide Hidden Groups from the Groups Loop
    add_filter(‘bp_groups_get_paged_groups_sql’, array($this, ‘filter_hidden_groups_sql’));
    add_filter(‘bp_groups_get_total_groups_sql’, array($this, ‘filter_hidden_groups_sql’));
    * If you’re doing this from functions.php instead of a class you’ll want:
    add_filter(‘bp_groups_get_paged_groups_sql’, ‘filter_hidden_groups_sql’);
    add_filter(‘bp_groups_get_total_groups_sql’, ‘filter_hidden_groups_sql’);

    And Execute your filter method to inject and AND for status != ‘hidden’:
    // Buddypress Filter Hidden Groups from Groups Loop
    function filter_hidden_groups_sql($sql) {
    $sql_parts = explode(‘WHERE’, $sql);
    $new_sql = $sql_parts[0] . “WHERE g.status != ‘hidden’ AND” . $sql_parts[1];
    return $new_sql;
    }

    This will filter the sql query retrieving groups to exclude any hidden groups making it so a Members Group Listing doesn’t have holes or invalid count numbers.

    Hope this is useful to someone,
    Cheers

Viewing 1 replies (of 1 total)
  • @garrett-eclipse

    Participant

    Update – Found an issue with my code when you go to the second page of the Members Groups list it reverts to showing hidden, etc. This is due to the call being done with AJAX which is considered an admin so !is_admin is false. To correct this I’ve updated the if statement to check if DOING_AJAX:

    // Buddypress Filter Hidden Groups from Groups Loop
    function filter_hidden_groups_sql($sql) {
    	if (!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)){
            $sql_parts = explode('WHERE', $sql);
            $new_sql = $sql_parts[0] . "WHERE g.status != 'hidden' AND" . $sql_parts[1];
    	    return $new_sql;
    	} else {
    		return $sql;
    	}
    }

    Basically this allows the backend admin to list all groups but the front end hides the hidden ones.

    Cheers

Viewing 1 replies (of 1 total)
  • The topic ‘Filter Hidden Groups from the Groups Loop’ is closed to new replies.
Skip to toolbar