Title: WP_Object_Cache::add
Published: April 25, 2014
Last modified: August 20, 2026

---

# WP_Object_Cache::add( int|string $key, mixed $data, string $group = 'default', int $expire ): bool

## In this article

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

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

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

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wp_object_cache/add/?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'`.

Default:`'default'`

`$expire`intoptional

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

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

 bool True on success, false if cache key and group already exist.

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

    ```php
    public function add( $key, $data, $group = 'default', $expire = 0 ) {
    	if ( wp_suspend_cache_addition() ) {
    		return false;
    	}

    	if ( ! $this->is_valid_key( $key ) ) {
    		return false;
    	}

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

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

    	if ( $this->_exists( $id, $group ) ) {
    		return false;
    	}

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

[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/7.1/src/wp-includes/class-wp-object-cache.php#L198)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wp-object-cache.php#L198-L221)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_object_cache/add/?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.

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

Serves as a utility function to determine whether a key exists in the cache.

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

Sets the data contents into the cache.

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

Temporarily suspends cache additions.

  |

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

Adds multiple values to the cache in one call.

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

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

  |

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

| Version | Description | 
| [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%2Fadd%2F)
before being able to contribute a note or feedback.