# update_meta_cache()

URL: https://chugunov.pro/api-wordpress/functions/update_meta_cache/
Проверено на WordPress 6.9, обновлено 06.08.2026.
Источник: независимый русскоязычный справочник chugunov.pro. Не является официальной документацией WordPress.

Тип: функция.
Появился в версии: 2.9.0.

## Сигнатура

```php
update_meta_cache( string $meta_type, string|int[] $object_ids ): array|false
```

## Описание

Обновляет кэш метаданных для указанных объектов.

## Параметры

- `$meta_type` `string` — обязательный. Тип объекта, к которому относятся метаданные. Принимает 'blog', 'post', 'comment', 'term', 'user' или любой другой тип объекта со связанной таблицей метаданных.
- `$object_ids` `string|int[]` — обязательный. Массив или список ID объектов через запятую, для которых нужно обновить кэш.

## Возвращаемое значение

`array|false`

## Исходный код

Файл: `wp-includes/meta.php:1137`

```php
function update_meta_cache( $meta_type, $object_ids ) {
	global $wpdb;

	if ( ! $meta_type || ! $object_ids ) {
		return false;
	}

	$table = _get_meta_table( $meta_type );
	if ( ! $table ) {
		return false;
	}

	$column = sanitize_key( $meta_type . '_id' );

	if ( ! is_array( $object_ids ) ) {
		$object_ids = preg_replace( '|[^0-9,]|', '', $object_ids );
		$object_ids = explode( ',', $object_ids );
	}

	$object_ids = array_map( 'intval', $object_ids );

	/**
	 * Short-circuits updating the metadata cache of a specific type.
	 *
	 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type
	 * (blog, post, comment, term, user, or any other type with an associated meta table).
	 * Returning a non-null value will effectively short-circuit the function.
	 *
	 * Possible hook names include:
	 *
	 *  - `update_blog_metadata_cache`
	 *  - `update_post_metadata_cache`
	 *  - `update_comment_metadata_cache`
	 *  - `update_term_metadata_cache`
	 *  - `update_user_metadata_cache`
	 *
	 * @since 5.0.0
	 *
	 * @param mixed $check      Whether to allow updating the meta cache of the given type.
	 * @param int[] $object_ids Array of object IDs to update the meta cache for.
	 */
	$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
	if ( null !== $check ) {
		return (bool) $check;
	}

	$cache_group    = $meta_type . '_meta';
	$non_cached_ids = array();
	$cache          = array();
	$cache_values   = wp_cache_get_multiple( $object_ids, $cache_group );

	foreach ( $cache_values as $id => $cached_object ) {
		if ( false === $cached_object ) {
			$non_cached_ids[] = $id;
		} else {
			$cache[ $id ] = $cached_object;
		}
	}

	if ( empty( $non_cached_ids ) ) {
		return $cache;
	}

	// Get meta info.
	$id_list   = implode( ',', $non_cached_ids );
	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';

	$meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A );

	if ( ! empty( $meta_list ) ) {
		foreach ( $meta_list as $metarow ) {
			$mpid = (int) $metarow[ $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;
		}
	}

	$data = array();
	foreach ( $non_cached_ids as $id ) {
		if ( ! isset( $cache[ $id ] ) ) {
			$cache[ $id ] = array();
		}
		$data[ $id ] = $cache[ $id ];
	}
	wp_cache_add_multiple( $data, $cache_group );

	return $cache;
}
```

## История изменений

- 2.9.0 — Introduced.

## Связанные

Использует: [`wp_cache_add_multiple`](https://chugunov.pro/api-wordpress/functions/wp_cache_add_multiple/), [`wp_cache_get_multiple`](https://chugunov.pro/api-wordpress/functions/wp_cache_get_multiple/), [`_get_meta_table`](https://chugunov.pro/api-wordpress/functions/_get_meta_table/), [`sanitize_key`](https://chugunov.pro/api-wordpress/functions/sanitize_key/), [`apply_filters`](https://chugunov.pro/api-wordpress/functions/apply_filters/), `wpdb::get_results`.
Используется в: `WP_Metadata_Lazyloader::lazyload_meta_callback`, [`get_metadata_raw`](https://chugunov.pro/api-wordpress/functions/get_metadata_raw/), [`update_sitemeta_cache`](https://chugunov.pro/api-wordpress/functions/update_sitemeta_cache/), [`update_termmeta_cache`](https://chugunov.pro/api-wordpress/functions/update_termmeta_cache/), [`cache_users`](https://chugunov.pro/api-wordpress/functions/cache_users/), [`get_userdata`](https://chugunov.pro/api-wordpress/functions/get_userdata/), [`get_user_metavalues`](https://chugunov.pro/api-wordpress/functions/get_user_metavalues/), [`update_postmeta_cache`](https://chugunov.pro/api-wordpress/functions/update_postmeta_cache/), [`update_comment_cache`](https://chugunov.pro/api-wordpress/functions/update_comment_cache/), [`metadata_exists`](https://chugunov.pro/api-wordpress/functions/metadata_exists/).

Оригинал в официальной документации: https://developer.wordpress.org/reference/functions/update_meta_cache/
