bp_admin_repair_group_count

Recalculate group counts for each user.

Description

(array) bp_admin_repair_group_count(); 

Returns (array)


Usage

  1. if ( !function_exists( 'bp_admin_repair_group_count' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-core/admin/bp-core-admin-tools.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_admin_repair_group_count(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/bp-core/admin/bp-core-admin-tools.php  
  1. function bp_admin_repair_group_count() { 
  2. global $wpdb; 
  3.  
  4. if ( ! bp_is_active( 'groups' ) ) { 
  5. return; 
  6.  
  7. $statement = __( 'Counting the number of groups for each user… %s', buddypress ); 
  8. $result = __( 'Failed!', buddypress ); 
  9.  
  10. $sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_group_count' );"; 
  11. if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) { 
  12. return array( 1, sprintf( $statement, $result ) ); 
  13.  
  14. $bp =buddypress); 
  15.  
  16. // Walk through all users on the site. 
  17. $total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c; 
  18.  
  19. if ( $total_users > 0 ) { 
  20. $per_query = 500; 
  21. $offset = 0; 
  22. while ( $offset < $total_users ) { 
  23. // But only bother to update counts for users that have groups. 
  24. $users = $wpdb->get_col( $wpdb->prepare( "SELECT user_id FROM {$bp->groups->table_name_members} WHERE is_confirmed = 1 AND is_banned = 0 AND user_id > %d AND user_id <= %d", $offset, $offset + $per_query ) ); 
  25.  
  26. foreach ( $users as $user ) { 
  27. BP_Groups_Member::refresh_total_group_count_for_user( $user ); 
  28.  
  29. $offset += $per_query; 
  30. } else { 
  31. return array( 2, sprintf( $statement, $result ) ); 
  32.  
  33. return array( 0, sprintf( $statement, __( 'Complete!', buddypress ) ) );