Skip to:
Content
Pages
Categories
Search
Top
Bottom

Exclude groups from get_total_group_count


  • pandreas
    Participant

    @pandreas

    Hello all!

    I want some groups with specific slug (so the condition is wp_bp_groups.status not in (‘xxx’, ‘yyy’)) to not be included at total groups count.

    I found that the hook is bp_get_total_group_count but how I can filter it?

    Do I have to change the query to database?

    I have write this to functions.php and it works:

    function filter_bp_get_total_group_count()
    {
    global $wpdb;
    $hidden_sql = "WHERE slug not in ('health', 'social', 'cultury')";
    $bp = buddypress();
    return $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}" );
    };
    // add the filter
    add_filter( 'bp_get_total_group_count', 'filter_bp_get_total_group_count', 10, 1 );

    But this is the way?? Isn’t there something better??

    Thank you!

    Regards,

    Andreas

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

  • Henry Wright
    Moderator

    @henrywright

    Inside the function, there’s a bp_get_total_group_count filter hook which can be used. Something like this as an example:

    function my_group_count_filter( $count ) {
        // Do something with $count here.
        return $count;
    }
    add_filter( 'bp_get_total_group_count' ,'my_group_count_filter' );

    pandreas
    Participant

    @pandreas

    I have done the same thing, if you read my post.

    The question is about the function ‘my_group_count_filter’.

    Do I have to use a simple and direct query to database like this one that my function has?

    return $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}"

    Or is there any other way to pass excluded groups to my counter?

    Thank you!

    Andreas


    shanebp
    Moderator

    @shanebp

    Please use the code button when sharing code.

    If you only need a total count, and know that there are 3 groups you don’t want included, then use Henry’s approach with this tweak: return $count - 3;

    Otherwise, don’t use a filter in the manner shown in your initial post.
    It will result in a double query.
    Just make it a custom function that you call wherever you want.


    Henry Wright
    Moderator

    @henrywright

    Considering you need to use a custom where clause to get your data, you should do what @shanebp suggests here:

    Just make it a custom function that you call wherever you want.

    My tweak only makes sense when a simple adjustment to $count is needed.


    pandreas
    Participant

    @pandreas

    Thank you for your answers and recommendations!

    The custom function that I have written before, I suppose it does what I want very well. Am I right?

    Here it is again.

    function filter_bp_get_total_group_count()
    {
    global $wpdb;
    $hidden_sql = "WHERE slug not in ('health', 'social', 'cultury')";
    $bp = buddypress();
    return $wpdb->get_var( "SELECT COUNT(id) FROM {$bp->groups->table_name} {$hidden_sql}" );
    };

    Henry Wright
    Moderator

    @henrywright

    You might also want to exclude hidden groups, just as the default group count function does. For example:

    status != 'hidden'


    pandreas
    Participant

    @pandreas

    Thank you, I don’t have to include hidden groups indeed!

    One more question: What about bp_get_total_group_count_for_user?

    How I can override this in order to exclude some groups from the count?

    Andreas


    Henry Wright
    Moderator

    @henrywright

    You can use the bp_get_total_group_count_for_user filter. For example:

    function my_func( $count, $user_id ) {
        // Do something with $count, using $user_id if you wish.
        return $count;
    }
    add_filter( 'bp_get_total_group_count_for_user', 'my_func', 10, 2 );

    pandreas
    Participant

    @pandreas

    Ok thank you!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Exclude groups from get_total_group_count’ is closed to new replies.
Skip to toolbar