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; }