wp_cache_get_salted()wp-includes/cache-compat.php |
Retrieves cached data if valid and unchanged.
|
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.
|
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_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::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.
|
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.
|
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.
|
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_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_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.
|
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.
|
Skip to note 5 content
sun
Here is how you use the
$foundparameter 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:$value = wp_cache_get( 'wpdocs_mykey', 'wpdocs_mygroup', false, $found ); if ( ! $found) { $value = wpdocs_my_expensive_function(); wp_cache_set( 'wpdocs_mykey', $value, 'wpdocs_mygroup' ); } return $value;You need to use the
$foundparameter 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
$foundparameter.If some object caches are not implementing the
$foundparameter 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.Skip to note 6 content
Mayeenul Islam
function prefix_get_post_count( $post_status = 'publish' ) { $cache_key = 'prefix_post_count_'. $post_status; $_posts = wp_cache_get( $cache_key ); if ( false === $_posts ) { $_posts = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = %s", $post_status )); wp_cache_set( $cache_key, $_posts ); } return $_posts; }Skip to note 7 content
Aamer Shahzad
/** * Handles the users column output. * * @since 4.3.0 * * @param array $blog Current site. */ public function column_users( $blog ) { $user_count = wp_cache_get( $blog['blog_id'] . '_user_count', 'blog-details' ); if ( ! $user_count ) { $blog_users = new WP_User_Query( array( 'blog_id' => $blog['blog_id'], 'fields' => 'ID', 'number' => 1, 'count_total' => true, ) ); $user_count = $blog_users->get_total(); wp_cache_set( $blog['blog_id'] . '_user_count', $user_count, 'blog-details', 12 * HOUR_IN_SECONDS ); } printf( '<a href="%s">%s</a>', esc_url( network_admin_url( 'site-users.php?id=' . $blog['blog_id'] ) ), number_format_i18n( $user_count ) ); }get_total()will always be zero or one, because the query is limited to one user per page of results with thenumberparameter.Skip to note 8 content
Michael Nelson
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]