bp_update_meta_cache( array $args = array() )

Update the metadata cache for the specified objects.


Description Description

Based on WordPress’s update_meta_cache(), this function primes the cache with metadata related to a set of objects. This is typically done when querying for a loop of objects; pre-fetching metadata for each queried object can lead to dramatic performance improvements when using metadata in the context of template loops.


Parameters Parameters

$args

(Optional) Array of arguments.

  • 'object_ids'
    (array|string) List of object IDs to fetch metadata for. Accepts an array or a comma-separated list of numeric IDs.
  • 'object_type'
    (string) The type of object, eg 'groups' or 'activity'.
  • 'meta_table'
    (string) The name of the metadata table being queried.
  • 'object_column'
    (string) Optional. The name of the database column where IDs (those provided by $object_ids) are found. Eg, 'group_id' for the groups metadata tables. Default: $object_type . '_id'.
  • 'cache_key_prefix'
    (string) Optional. The prefix to use when creating cache key names. Default: the value of $meta_table.

Default value: array()


Top ↑

Return Return

(false|array) Metadata cache for the specified objects, or false on failure.


Top ↑

Source Source

File: bp-core/bp-core-cache.php

function bp_update_meta_cache( $args = array() ) {
	global $wpdb;

	$defaults = array(
		'object_ids' 	   => array(), // Comma-separated list or array of item ids.
		'object_type' 	   => '',      // Canonical component id: groups, members, etc.
		'cache_group'      => '',      // Cache group.
		'meta_table' 	   => '',      // Name of the table containing the metadata.
		'object_column'    => '',      // DB column for the object ids (group_id, etc).
		'cache_key_prefix' => ''       // Prefix to use when creating cache key names. Eg 'bp_groups_groupmeta'.
	);
	$r = wp_parse_args( $args, $defaults );
	extract( $r );

	if ( empty( $object_ids ) || empty( $object_type ) || empty( $meta_table ) || empty( $cache_group ) ) {
		return false;
	}

	if ( empty( $cache_key_prefix ) ) {
		$cache_key_prefix = $meta_table;
	}

	if ( empty( $object_column ) ) {
		$object_column = $object_type . '_id';
	}

	if ( ! $cache_group ) {
		return false;
	}

	$object_ids   = wp_parse_id_list( $object_ids );
	$uncached_ids = bp_get_non_cached_ids( $object_ids, $cache_group );

	$cache = array();

	// Get meta info.
	if ( ! empty( $uncached_ids ) ) {
		$id_list   = join( ',', wp_parse_id_list( $uncached_ids ) );
		$meta_list = $wpdb->get_results( esc_sql( "SELECT {$object_column}, meta_key, meta_value FROM {$meta_table} WHERE {$object_column} IN ({$id_list})" ), ARRAY_A );

		if ( ! empty( $meta_list ) ) {
			foreach ( $meta_list as $metarow ) {
				$mpid = intval( $metarow[$object_column] );
				$mkey = $metarow['meta_key'];
				$mval = $metarow['meta_value'];

				// Force subkeys to be array type.
				if ( !isset( $cache[$mpid] ) || !is_array( $cache[$mpid] ) )
					$cache[$mpid] = array();
				if ( !isset( $cache[$mpid][$mkey] ) || !is_array( $cache[$mpid][$mkey] ) )
					$cache[$mpid][$mkey] = array();

				// Add a value to the current pid/key.
				$cache[$mpid][$mkey][] = $mval;
			}
		}

		foreach ( $uncached_ids as $uncached_id ) {
			// Cache empty values as well.
			if ( ! isset( $cache[ $uncached_id ] ) ) {
				$cache[ $uncached_id ] = array();
			}

			wp_cache_set( $uncached_id, $cache[ $uncached_id ], $cache_group );
		}
	}

	return $cache;
}

Top ↑

Changelog Changelog

Changelog
Version Description
1.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.