wp_cache_get()WP 2.0.0

Retrieves the object cache data by specified cache key and group.

All cache-related work is made using instance of the WP_Object_Cache class. The cache data is saved to the memory (server RAM) during the page generation. But if you're using a persistent cache plugin, the cache can be stored among page visits for a longer amount of time.

See also:

  • wp_cache_add() — adds a new data to the object cache; does nothing if the cache with this key already exists.
  • wp_cache_set() — saves the data to the object cache.
1 time — 0.000001 sec (speed of light) | 50000 times — 0.054485 sec (speed of light) | PHP 7.1.11, WP 4.9.8

No Hooks.


Mixed|false. The cache contents on success, false on failure to retrieve contents.


wp_cache_get( $key, $group, $force, $found );
$key(int|string) (required)
The key under which the cache contents are stored.
Where the cache contents are grouped.
Default: ''
Whether to force an update of the local cache from the persistent cache.
Default: false
$found(true|false) (passed by reference — &)
Whether the key was found in the cache (passed by reference). Disambiguates a return of false, a storable value.
Default: null



#1 Retrieve the data from the cache

By default, most of the data is stored in the cache. For example, WordPress options are added to the cache when retrieved for the first time, and all the following times, this option will be retrieved from the cache, not from DB. This is made to minimize the number of database requests.

This is how we can retrieve the user's metadata from the cache:

$user_meta = wp_cache_get( 1, 'user_meta' );

#2 Save data to the cache and retrieve it when we need it

Suppose we have made a heavy database query and its result is needed in the different parts of the page. We can save it to the cache and access every time we need it. Thus, we eliminate the need to do the same database query many times for a single page:

$cache_key = 'my_db_result';

// Do a database query and save it to the cache if the there is no cache data with this key:
$my_db_result = wp_cache_get( $cache_key );
if( false === $my_db_result ){
	global $wpdb;
	$my_db_result = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_type = 'books'");

	wp_cache_set( $cache_key, $my_db_result );

// $my_db_result has the same content as the cache

print_r( $my_db_result );

Such cache saving makes sense only if your site has an object caching plugin installed.



Since 2.0.0 Introduced.

wp_cache_get() code WP 6.5.4

function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
	global $wp_object_cache;

	return $wp_object_cache->get( $key, $group, $force, $found );