wp_cache_set( int|string $key, mixed $data, string $group = '', int $expire ): bool

Saves the data to the cache.


Description

Differs from wp_cache_add() and wp_cache_replace() in that it will always write data.

Top ↑

See also


Top ↑

Parameters

$key int|string Required
The cache key to use for retrieval later.
$data mixed Required
The contents to store in the cache.
$group string Optional
Where to group the cache contents. Enables the same key to be used across groups.

Default: ''

$expire int Optional
When to expire the cache contents, in seconds.
Default 0 (no expiration).

Top ↑

Return

bool True on success, false on failure.


Top ↑

Source

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

function wp_cache_set( $key, $data, $group = '', $expire = 0 ) {
	global $wp_object_cache;

	return $wp_object_cache->set( $key, $data, $group, (int) $expire );
}


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;
    }

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