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

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


Description

Top ↑

See also


Top ↑

Parameters

$key int|string Required
The key under which the cache contents are stored.
$group string Optional
Where the cache contents are grouped.

Default: ''

$force bool Optional
Whether to force an update of the local cache from the persistent cache.

Default: false

$found bool Optional
Whether the key was found in the cache (passed by reference).
Disambiguates a return of false, a storable value.

Default: null


Top ↑

Return

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


Top ↑

Source

File: wp-includes/cache.php. View all references

function wp_cache_get( $key, $group = '', $force = false, &$found = null ) {
	global $wp_object_cache;

	return $wp_object_cache->get( $key, $group, $force, $found );
}


Top ↑

Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by 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;
    }
  2. Skip to note 2 content
    Contributed by 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 )
        );
    }
  3. Skip to note 3 content
    Contributed by 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]

You must log in before being able to contribute a note or feedback.