wppaste
WordPress

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

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

Reads a value stored under a given key and cache group, returning false when nothing is found. Because false is also a legitimate cached value, the fourth parameter $found is the only reliable way to tell a stored false apart from a genuine cache miss. Pair it with wp_cache_set() to write the value in the first place.

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
$foundbool|nulloptional
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 calculation and check whether it was actually stored

Cache the word count of post 1's content under a custom group so a page render doesn't recompute it on every call.

$post = get_post( 1 );
$cache_key = 'word_count_' . $post->ID;
$cache_group = 'my_plugin_stats';

$found = null;
$count = wp_cache_get( $cache_key, $cache_group, false, $found );

if ( ! $found ) {
	printf( "Cache miss, computing now.\n" );
	$count = str_word_count( wp_strip_all_tags( $post->post_content ) );
	wp_cache_set( $cache_key, $count, $cache_group );
} else {
	printf( "Cache hit, reused stored value.\n" );
}

echo esc_html( sprintf( 'Post %d has %d words (found flag: %s)', $post->ID, $count, var_export( $found, true ) ) );

On a fresh install with no persistent object cache drop-in, this only avoids recomputation within the same request, it does not survive a page reload.

Distinguish a cached false value from a true cache miss

Cache the boolean result of a price check on post 2's meta, which can legitimately be false, and show why checking the return value alone is unsafe.

$price = get_post_meta( 2, 'price', true );
$has_price = ! empty( $price );

wp_cache_set( 'post_2_has_price', $has_price, 'pricing_flags' );

$found = null;
$cached = wp_cache_get( 'post_2_has_price', 'pricing_flags', false, $found );

if ( false === $cached && ! $found ) {
	echo esc_html( 'No cached value exists yet for this key.' );
} elseif ( false === $cached && $found ) {
	echo esc_html( 'Cached value is false, and it really is false, not missing.' );
} else {
	echo esc_html( sprintf( 'Cached value: %s', var_export( $cached, true ) ) );
}

Common problems and fixes · 4

Why does wp_cache_get() return the right value in my code but nothing shows up on the next page load?

wp_cache_get() reads from $wp_object_cache, and on a default install without a persistent object cache drop-in that object only lives for the current request. Anything written with wp_cache_set() disappears as soon as the request ends.

How do I tell a cached false value apart from a cache miss?

The function returns false in both cases, so testing the return value alone is ambiguous. Pass a variable by reference as the fourth argument instead:

What does the $force parameter actually do?

It only matters when an external persistent object cache is active, forcing a fresh read from the persistent layer instead of the in-process local cache. On the default non-persistent object cache there is only one layer, so passing true has no visible effect.

Why is my cached value showing up under the wrong feature?

Leaving $group as the default empty string puts every unrelated call into the same shared group, so two features using the same key string will overwrite each other's cache.

Alternatives and related functions

wp_cache_set
When you need to write or refresh the value that a later wp_cache_get() call should retrieve.
wp_cache_add
When you want to store a value only if the key does not already exist, instead of overwriting it.
wp_cache_delete
When cached data has become stale and needs to be invalidated so the next wp_cache_get() forces a miss.
get_transient
When the cached value must survive across requests even without a persistent object cache, since transients fall back to the options table.

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 7.0.4 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.