Retrieves the cache contents from the cache by key and group.
Description
See also
Parameters
$key
int|stringrequired- The key under which the cache contents are stored.
$group
stringoptional- Where the cache contents are grouped.
Default:
''
$force
booloptional- Whether to force an update of the local cache from the persistent cache.
Default:
false
$found
booloptional- Whether the key was found in the cache (passed by reference).
Disambiguates a return of false, a storable value.Default:
null
Return
mixed|false The cache contents on success, false on failure to retrieve contents.Source
function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
global $wp_object_cache;
return $wp_object_cache->get( $key, $group, $force, $found );
}
Related
Uses | Description |
---|---|
WP_Object_Cache::get()wp-includes/class-wp-object-cache.php | Retrieves the cache contents, if it exists. |
Used by | Description |
---|---|
wp_prime_network_option_caches()wp-includes/option.php | Primes specific network options into the cache with a single database query. |
WP_Textdomain_Registry::get_language_files_from_path()wp-includes/class-wp-textdomain-registry.php | Retrieves translation files from the specified path. |
wp_prime_option_caches()wp-includes/option.php | Primes specific options into the cache with a single database query. |
wp_get_theme_data_template_parts()wp-includes/global-styles-and-settings.php | Returns the metadata for the template parts defined by the theme. |
wp_cache_set_last_changed()wp-includes/functions.php | Sets last changed date for the specified cache group to now. |
wp_get_global_styles_custom_css()wp-includes/deprecated.php | Gets the global styles custom CSS from theme.json. |
wp_get_global_styles_svg_filters()wp-includes/deprecated.php | Returns a string containing the SVGs to be referenced as filters (duotone). |
wp_get_global_settings()wp-includes/global-styles-and-settings.php | Gets the settings resulting of merging core, theme, and user data. |
wp_get_global_stylesheet()wp-includes/global-styles-and-settings.php | Returns the stylesheet resulting of merging core, theme, and user data. |
get_metadata_raw()wp-includes/meta.php | Retrieves raw metadata value for the specified object. |
_find_post_by_old_slug()wp-includes/query.php | Find the post ID for redirecting an old slug. |
_find_post_by_old_date()wp-includes/query.php | Find the post ID for redirecting an old date. |
WP_Privacy_Requests_Table::get_request_counts()wp-admin/includes/class-wp-privacy-requests-table.php | Counts the number of requests for each status. |
WP_Embed::find_oembed_post_id()wp-includes/class-wp-embed.php | Finds the oEmbed cache post ID for a given cache key. |
WP_Customize_Manager::find_changeset_post_id()wp-includes/class-wp-customize-manager.php | Finds the changeset post ID for a given changeset UUID. |
wp_cache_get_last_changed()wp-includes/functions.php | Gets last changed date for the specified cache group. |
WP_Term_Query::get_terms()wp-includes/class-wp-term-query.php | Retrieves the query results. |
WP_Network_Query::get_networks()wp-includes/class-wp-network-query.php | Gets a list of networks matching the query vars. |
WP_Site::get_details()wp-includes/class-wp-site.php | Retrieves the details for this site. |
ms_load_current_site_and_network()wp-includes/ms-load.php | Identifies the network and site of a requested domain and path and populates the corresponding network and site global objects as part of the multisite bootstrap process. |
WP_Site_Query::get_sites()wp-includes/class-wp-site-query.php | Retrieves a list of sites matching the query vars. |
WP_Site::get_instance()wp-includes/class-wp-site.php | Retrieves a site from the database by its ID. |
WP_Network::get_instance()wp-includes/class-wp-network.php | Retrieves a network from the database by its ID. |
WP_Comment::get_instance()wp-includes/class-wp-comment.php | Retrieves a WP_Comment instance. |
WP_Term::get_instance()wp-includes/class-wp-term.php | Retrieve WP_Term instance. |
update_network_option()wp-includes/option.php | Updates the value of a network option that was already added. |
add_network_option()wp-includes/option.php | Adds a new network option. |
delete_network_option()wp-includes/option.php | Removes a network option by name. |
get_network_option()wp-includes/option.php | Retrieves a network’s option value based on the option name. |
WP_MS_Sites_List_Table::column_users()wp-admin/includes/class-wp-ms-sites-list-table.php | Handles the users column output. |
WP_Comment_Query::get_comments()wp-includes/class-wp-comment-query.php | Get a list of comments matching the query vars. |
get_plugins()wp-admin/includes/plugin.php | Checks the plugins directory and retrieve all plugin files with plugin data. |
WP_User::get_data_by()wp-includes/class-wp-user.php | Returns only the main user fields. |
wp_get_archives()wp-includes/general-template.php | Displays archive links based on type and format. |
get_calendar()wp-includes/general-template.php | Displays calendar with days that have posts as links. |
get_usermeta()wp-includes/deprecated.php | Retrieve user metadata. |
WP_Theme::cache_get()wp-includes/class-wp-theme.php | Gets theme data from cache. |
WP_Query::get_posts()wp-includes/class-wp-query.php | Retrieves an array of posts based on query variables. |
is_blog_installed()wp-includes/functions.php | Determines whether WordPress is already installed. |
get_object_term_cache()wp-includes/taxonomy.php | Retrieves the cached term objects for the given object ID. |
get_objects_in_term()wp-includes/taxonomy.php | Retrieves object IDs of valid taxonomy and term. |
get_adjacent_post()wp-includes/link-template.php | Retrieves the adjacent post. |
get_site_transient()wp-includes/option.php | Retrieves the value of a site transient. |
get_transient()wp-includes/option.php | Retrieves the value of a transient. |
wp_load_alloptions()wp-includes/option.php | Loads and caches all autoloaded options, if available or all options. |
update_option()wp-includes/option.php | Updates the value of an option that was already added. |
add_option()wp-includes/option.php | Adds a new option. |
delete_option()wp-includes/option.php | Removes an option by name. Prevents removal of protected WordPress options. |
get_option()wp-includes/option.php | Retrieves an option value based on an option name. |
WP_User_Query::query()wp-includes/class-wp-user-query.php | Executes the query, with the current variables. |
WP_Post::get_instance()wp-includes/class-wp-post.php | Retrieve WP_Post instance. |
wp_mime_type_icon()wp-includes/post.php | Retrieves the icon for a MIME type or attachment. |
_get_last_post_time()wp-includes/post.php | Gets the timestamp of the last time any post was modified or published. |
get_all_page_ids()wp-includes/post.php | Gets a list of page IDs. |
get_page_by_path()wp-includes/post.php | Retrieves a page given its path. |
wp_count_posts()wp-includes/post.php | Counts number of posts of a post type and if user has permissions to view. |
wp_count_attachments()wp-includes/post.php | Counts number of attachments for the mime type(s). |
get_blog_id_from_url()wp-includes/ms-functions.php | Gets a blog’s numeric ID from its URL. |
get_bookmark()wp-includes/bookmark.php | Retrieves bookmark data. |
get_bookmarks()wp-includes/bookmark.php | Retrieves the list of bookmarks. |
get_blog_details()wp-includes/ms-blogs.php | Retrieves the details for a blog from the blogs table and blog options. |
wp_count_comments()wp-includes/comment.php | Retrieves the total comment counts for the whole site or a single post. |
get_lastcommentmodified()wp-includes/comment.php | Retrieves the date the last comment was modified. |
metadata_exists()wp-includes/meta.php | Determines if a meta field with the given key exists for the given object ID. |
Changelog
Version | Description |
---|---|
2.0.0 | Introduced. |
Here is how you use the
$found
parameter in your code – it is passed by reference, so you do not need to define the variable upfront, its value will be set by the called function instead:You need to use the
$found
parameter if there is a slightest chance that the value you are caching can be zero, false, null, or otherwise equivalent to false (falsy) like an empty array.If you are not sure, or if you are looking for a general rule of thumb – always use the
$found
parameter.If some object caches are not implementing the
$found
parameter correctly, then that is a good reason to fix those implementations (or use better maintained ones) and not a reason to not use the parameter.get_total()
will always be zero or one, because the query is limited to one user per page of results with thenumber
parameter.It seems some cache dropins (like those used on WPEngine) may define `wp_cache_get` WITHOUT the 4th parameter, `$found`. This means `$found` might not be populated by `wp_cache_get() ` (it will always be `null`, regardless of whether the value was cached or not), and you can also get a ton of warnings as reported here and here.
The workaround? Pretend `$found` isn’t an argument, and never use `wp_cache_set() ` to cache a value of `false` (because you won’t be able to differentiate a cached value of `false` from the lack of a cached value, where `wp_cache_get() ` also returns `false`.)
Eg
[PHP]
$result = wp_get_cache(‘my_key’);
if( $result === false){
// calculate the result, and use `0` or `null` instead of `false`
if( $result === false){
$result = 0;
}
wp_cache_set(‘my_key’, $result);
}
[/php]