bp_groups_prefetch_activity_object_data( array $activities )

Fetch data related to groups at the beginning of an activity loop.

Description

This reduces database overhead during the activity loop.

Parameters

$activities

(Required) Array of activity items.

Return

(array)

Source

File: bp-groups/bp-groups-activity.php

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
function bp_groups_prefetch_activity_object_data( $activities ) {
    $group_ids = array();
 
    if ( empty( $activities ) ) {
        return $activities;
    }
 
    foreach ( $activities as $activity ) {
        if ( buddypress()->groups->id !== $activity->component ) {
            continue;
        }
 
        $group_ids[] = $activity->item_id;
    }
 
    if ( ! empty( $group_ids ) ) {
 
        // TEMPORARY - Once the 'populate_extras' issue is solved
        // in the groups component, we can do this with groups_get_groups()
        // rather than manually.
        $uncached_ids = array();
        foreach ( $group_ids as $group_id ) {
            if ( false === wp_cache_get( $group_id, 'bp_groups' ) ) {
                $uncached_ids[] = $group_id;
            }
        }
 
        if ( ! empty( $uncached_ids ) ) {
            global $wpdb;
            $bp = buddypress();
            $uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) );
            $groups = $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name} WHERE id IN ({$uncached_ids_sql})" );
            foreach ( $groups as $group ) {
                wp_cache_set( $group->id, $group, 'bp_groups' );
            }
        }
    }
 
    return $activities;
}

Changelog

Changelog
Version Description
BuddyPress 2.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.