WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/set transient

Description

Set/update the value of a transient.

You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is set.

Usage

<?php set_transient$transient$value$expiration ); ?>

Parameters

$transient
(string) (required) Transient name. Expected to not be SQL-escaped. If memcached is not enabled the name should be 172 characters or less in length as WordPress will prefix your name with "_transient_" or "_transient_timeout_" in the options table (depending on whether it expires or not). Longer key names will silently fail. See Trac #15058.
Default: None
$value
(mixed) (required) Transient value. Expected to not be SQL-escaped.
Default: None
$expiration
(int) (optional) Time until expiration in seconds from now, or 0 for never expires. Ex: For one day, the expiration value would be: (60 * 60 * 24) Note: transients can be removed BEFORE the expiration time. See the Transient API Overview.
Default: 0.

Return Values

(boolean) 
False if value was not set and true if value was set.

Examples

Saving the $special_query_results object for 12 hours

<?php
set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );
?>

Notes

If a transient exists, this function will update the transient's expiration time.

NB: transients that never expire are autoloaded, whereas transients with an expiration time are not autoloaded. Consider this when adding transients that may not be needed on every page, and thus do not need to be autoloaded, impacting page performance.

WordPress provides some constants for specifying time in seconds. Instead of multiplying out integers, see Transients_API#Using_Time_Constants.

Transient key names are limited to 191 characters due to the database schema in the wp_options table ( option_name: varchar(191) ).

In WordPress versions previous to 4.4, the length limitation was 45 in set_transient (now 172) and 64 in the database (now 191).

Change Log

Since: 2.8

Source File

set_transient() is located in wp-includes/option.php.

Related

Transients API: set_transient(), get_transient(), delete_transient(), set_site_transient(), get_site_transient(), delete_site_transient()

See also index of Function Reference and index of Template Tags.