wppaste
WordPress

wp_cache_get( int|string $key, string $group = '', bool $force = false, bool $found = null ): mixed|false

Since
2.0.0
Source
wp-includes/cache.php:151

Reads a previously stored value out of WordPress's object cache using a key and an optional group. Pass a variable by reference as $found to tell a cached false apart from a cache miss. Pair it with wp_cache_set() to actually populate the cache, since wp_cache_get() never writes anything itself.

Retrieves the cache contents from the cache by key and group.

Compatibility

WordPress
since 2.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$keyint|string
The key under which the cache contents are stored.
$groupstringoptional
Where the cache contents are grouped. Default empty.Default: ''
$forcebooloptional
Whether to force an update of the local cache from the persistent cache. Default false.Default: false
$foundbooloptional
Whether the key was found in the cache (passed by reference).
Disambiguates a return of false, a storable value. Default null.Default: null

Return value

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

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Cache an expensive per-post calculation with wp_cache_get and wp_cache_set

Compute a marked-up price from post meta once per group/key and reuse it on subsequent calls within the same request.

$post_id = 2;
$group   = 'shop_pricing';
$found   = null;

$cached_price = wp_cache_get( $post_id, $group, false, $found );

if ( false === $found ) {
	$base_price = (float) get_post_meta( $post_id, 'price', true );
	$marked_up  = $base_price * 1.2;
	wp_cache_set( $post_id, $marked_up, $group );
	echo esc_html( sprintf( 'Not cached yet, calculated and stored: %s', $marked_up ) );
} else {
	echo esc_html( sprintf( 'Read from cache: %s', $cached_price ) );
}

On a fresh request this always prints the calculated branch because nothing has populated the group yet; run the code twice in the same request to see the cached branch.

Tell a cached false value apart from a cache miss

Store a literal false in a group, then use the $found argument to confirm it really came from the cache.

wp_cache_set( 'is_featured', false, 'demo_flags' );

$found = null;
$value = wp_cache_get( 'is_featured', 'demo_flags', false, $found );

if ( true === $found ) {
	echo esc_html( 'Cache hit, stored value is: ' . var_export( $value, true ) );
} else {
	echo esc_html( 'Key not found in this cache group.' );
}

Common problems and fixes · 4

Why can't I tell if wp_cache_get returned false because nothing was cached or because the cached value really is false?

wp_cache_get() returns false on failure to retrieve contents, but false is also a perfectly valid value to store, so the return value alone is ambiguous. The function accepts $found by reference for exactly this reason.

Why does passing $force = true not refresh the value from my caching plugin?

wp_cache_get() just forwards $force to WP_Object_Cache::get(). On a standard install there is no persistent backend, so the object cache is only a PHP array for the current request and forcing a refresh has nothing external to pull from.

Why is my cached value missing when I fetch it with a different piece of code?

wp_cache_get() looks up the key inside a specific $group; if the code that wrote the value used a different group string (or the default empty group) than the code reading it, the lookup misses even though the key name matches.

Why did my cached data vanish between two separate page loads?

On a default WordPress install without a persistent object cache plugin, WP_Object_Cache stores everything in memory only for the lifetime of a single request, so nothing survives to the next page load.

Alternatives and related functions

wp_cache_set
When you need to write or update the value that wp_cache_get() will later read.
wp_cache_delete
When you need to invalidate a cached entry instead of reading it.
get_transient
When you need caching that survives across requests even without a persistent object cache backend.
WP_Object_Cache::get
When you are working inside the object cache implementation itself rather than calling the wp_cache_get() wrapper.

Performance profile

How much work a call to wp_cache_get() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Only touches the object cache, via wp_cache_get().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12

Executed per call on PHP 8.5. The body compiles to 12.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • cacheobject cachewp_cache_get()this function does it

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_cache_get() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always12->get()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 12 instructions, 12 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Used by · 50

Show all 50

Source code

function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {	global $wp_object_cache; 	return $wp_object_cache->get( $key, $group, $force, $found );}

Changelog

Introduced in 2.0.0. One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Parameter $found retyped from bool to bool|null.verified against source
2.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 tag, from src/wp-includes/cache.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.