Meta query count for Buddypress Group Members
-
I am trying to work something like this (below) to count all members in a group that have a certain meta value. I am sure there are several ways to do this, but i am new to php and buddypress and can see filter options for groups, or meta queries for all users, but have not figured out how to just run a query for members within a group. I am using this to display summary statistics in the group header.
Any help would be greatly appreciated.
$args = array( 'meta_query' => array( array( 'key' => ‘action_one_total’, 'value' => ’88’, 'compare' => ‘=‘ ), ), 'count_total' => true ); } $users = new WP_User_Query($args); print_r( $users->get_total() );
-
You don’t need the
meta_query
in your example.You can use one of these classes:
buddypress\bp-core\classes\class-bp-user-query.php
buddypress\bp-groups\classes\class-bp-group-member-query.php
Using the former class is more verbose but maybe easier to follow at first:
function group_members_counter() { $args = array( 'group_id' => 1, // bp_get_group_id(), or pass in a group id 'exclude_admins_mods' => false ); $group_members_result = groups_get_group_members( $args ); $group_members_ids = array(); foreach( $group_members_result['members'] as $member ) { $group_members_ids[] = $member->ID; } echo 'group member ids: ' . implode(", ", $group_members_ids); if ( ! empty ( $group_members_ids ) ) { $args = array( 'include' => $group_members_ids, 'meta_key' => 'action_one_total', 'meta_value' => '88', 'populate_extras' => false ); $members = new BP_User_Query( $args ); echo '<br>matching meta count: ' . $members->total_users; } else { echo 'There are no group members.'; } } add_action( 'bp_ready', 'group_members_counter' );
Shane this is brilliant and exactly what I was looking for!
…hours getting no where. I can’t thank you enough!
Hi everybody
The method that Shane provided, is really good. I am looking to add a similar feature to disable certain groups. I followed this guide and customized it to make a query that will filter out disabled groups. Everything seems to be working fine except the group count inside the timeline. From where can I find and edit the group count??
- You must be logged in to reply to this topic.