Title: WP_Object_Cache::set
Published: April 25, 2014
Last modified: February 24, 2026

---

# WP_Object_Cache::set( int|string $key, mixed $data, string $group, int $expire ): bool

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#wp--skip-link--target)

Sets the data contents into the cache.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#description)󠁿

The cache contents are grouped by the $group parameter followed by the $key. This
allows for duplicate IDs in unique groups. Therefore, naming of the group should
be used with care and should follow normal function naming guidelines outside of
core WordPress usage.

The $expire parameter is not used, because the cache will automatically expire for
each time a page is accessed and PHP finishes. The method is more for cache plugins
which use files.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#parameters)󠁿

 `$key`int|stringrequired

What to call the contents in the cache.

`$data`mixedrequired

The contents to store in the cache.

`$group`stringoptional

Where to group the cache contents. Default `'default'`.

`$expire`intoptional

Not used.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#return)󠁿

 bool True if contents were set, false if key is invalid.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#source)󠁿

    ```php
    public function set( $key, $data, $group = 'default', $expire = 0 ) {
    	if ( ! $this->is_valid_key( $key ) ) {
    		return false;
    	}

    	if ( empty( $group ) ) {
    		$group = 'default';
    	}

    	if ( $this->multisite && ! isset( $this->global_groups[ $group ] ) ) {
    		$key = $this->blog_prefix . $key;
    	}

    	if ( is_object( $data ) ) {
    		$data = clone $data;
    	}

    	$this->cache[ $group ][ $key ] = $data;
    	return true;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-object-cache.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-object-cache.php#L301)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-object-cache.php#L301-L320)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Object_Cache::is_valid_key()](https://developer.wordpress.org/reference/classes/wp_object_cache/is_valid_key/)`wp-includes/class-wp-object-cache.php` |

Serves as a utility function to determine whether a key is valid.

  |

| Used by | Description | 
| [WP_Object_Cache::set_multiple()](https://developer.wordpress.org/reference/classes/wp_object_cache/set_multiple/)`wp-includes/class-wp-object-cache.php` |

Sets multiple values to the cache in one call.

  | 
| [WP_Object_Cache::replace()](https://developer.wordpress.org/reference/classes/wp_object_cache/replace/)`wp-includes/class-wp-object-cache.php` |

Replaces the contents in the cache, if contents already exist.

  | 
| [WP_Object_Cache::add()](https://developer.wordpress.org/reference/classes/wp_object_cache/add/)`wp-includes/class-wp-object-cache.php` |

Adds data to the cache if it doesn’t already exist.

  | 
| [wp_cache_set()](https://developer.wordpress.org/reference/functions/wp_cache_set/)`wp-includes/cache.php` |

Saves the data to the cache.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/WP_Object_Cache/set/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.1.0](https://developer.wordpress.org/reference/since/6.1.0/) | Returns false if cache key is invalid. | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_object_cache%2Fset%2F)
before being able to contribute a note or feedback.