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.
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?
- Why does passing $force = true not refresh the value from my caching plugin?
- Why is my cached value missing when I fetch it with a different piece of code?
- Why did my cached data vanish between two separate page loads?
Why can't I tell if wp_cache_get returned false because nothing was cached or because the cached value really is false?
Why does passing $force = true not refresh the value from my caching plugin?
Why is my cached value missing when I fetch it with a different piece of code?
Why did my cached data vanish between two separate page loads?
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
- Scaling
- Constant
- Instructions
- 12
- Plugin surface
- None
- Called by
- 50
Only touches the object cache, via wp_cache_get().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 12.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- cacheobject cache
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12 | ->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
- Twenty_Eleven_Ephemera_Widget::update()Update widget settings.
- Twenty_Eleven_Ephemera_Widget::widget()Outputs the HTML for this widget.
- WP_Comment::get_instance()Retrieves a WP_Comment instance.
- WP_Comment_Query::get_comments()Get a list of comments matching the query vars.
- WP_Customize_Manager::find_changeset_post_id()Finds the changeset post ID for a given changeset UUID.
- WP_Embed::find_oembed_post_id()Finds the oEmbed cache post ID for a given cache key.
- WP_MS_Sites_List_Table::column_users()Handles the users column output.
- WP_Network::get_instance()Retrieves a network from the database by its ID.
- WP_Network_Query::get_networks()Gets a list of networks matching the query vars.
- WP_Post::get_instance()Retrieve WP_Post instance.
- WP_Privacy_Requests_Table::get_request_counts()Counts the number of requests for each status.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
Show all 50
- WP_Site::get_details()Retrieves the details for this site.
- WP_Site::get_instance()Retrieves a site from the database by its ID.
- WP_Site_Query::get_sites()Retrieves a list of sites matching the query vars.
- WP_Term::get_instance()Retrieve WP_Term instance.
- WP_Term_Query::get_terms()Retrieves the query results.
- WP_Textdomain_Registry::get_language_files_from_path()Retrieves translation files from the specified path.
- WP_Theme::cache_get()Gets theme data from cache.
- WP_User::get_data_by()Returns only the main user fields.
- WP_User_Query::query()Executes the query, with the current variables.
- _find_post_by_old_date()Find the post ID for redirecting an old date.
- _find_post_by_old_slug()Find the post ID for redirecting an old slug.
- _get_cron_lock()Retrieves the cron lock.
- _get_last_post_time()Gets the timestamp of the last time any post was modified or published.
- _wp_image_editor_choose()Tests which editors are capable of supporting the request.
- add_network_option()Adds a new network option.
- add_option()Adds a new option.
- delete_network_option()Removes a network option by name.
- delete_option()Removes an option by name. Prevents removal of protected WordPress options.
- get_adjacent_post()Retrieves the adjacent post.
- get_all_page_ids()Gets a list of page IDs.
- get_blog_details()Retrieves the details for a blog from the blogs table and blog options.
- get_blog_id_from_url()Gets a blog's numeric ID from its URL.
- get_bookmark()Retrieves bookmark data.
- get_bookmarks()Retrieves the list of bookmarks.
- get_calendar()Displays calendar with days that have posts as links.
- get_lastcommentmodified()Retrieves the date the last comment was modified.
- get_metadata_raw()Retrieves raw metadata value for the specified object.
- get_network_option()Retrieves a network's option value based on the option name.
- get_object_term_cache()Retrieves the cached term objects for the given object ID.
- get_objects_in_term()Retrieves object IDs of valid taxonomy and term.
- get_option()Retrieves an option value based on an option name.
- get_page_by_path()Retrieves a page given its path.
- get_plugins()Checks the plugins directory and retrieve all plugin files with plugin data.
- get_site_transient()Retrieves the value of a site transient.
- get_transient()Retrieves the value of a transient.
- get_usermeta()Retrieve user metadata.
- is_blog_installed()Determines whether WordPress is already installed.
- metadata_exists()Determines if a meta field with the given key exists for the given object ID.
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.
Signature, return type and hooks compared across 5 parsed releases.
$found retyped from bool to bool|null.verified against sourceAbout this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.