set_transient()WP 2.8.0

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.

Return

true|false. True if the value was set, false otherwise.

Usage

set_transient( $transient, $value, $expiration );
$transient(string) (required)
Transient name. Expected to not be SQL-escaped. Must be 172 characters or fewer in length.
$value(mixed) (required)
Transient value. Must be serializable if non-scalar. Expected to not be SQL-escaped.
$expiration(int)
Time until expiration in seconds.
Default: 0 (no expiration)

Examples

0

#1 Let's save the request

This example shows how to save the result of a complex SQL query to a transient option for 12 hours:

set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );

Next, use get_transient() to get the result.

0

#2 Let's save the request for the last 5 posts to the transit option

This example shows how to get the last 5 posts and save the result for one day to a transient option.

// make a request
$latest_post = new WP_Query( array(
	'post_type'      => 'post',
	'posts_per_page' => 5,
	'orderby'        => 'date',
	'order'          => 'DESC'
) );

// Save the results to a transit option named latest_5_posts
set_transient( 'latest_5_posts', $latest_post, DAY_IN_SECONDS );

See WP_Query for more parameters to retrieve posts.

0

#3 The option lifetime can only be changed by specifying the third parameter and after expiration time is ended

Everything described below is true only if the external object cache is not used.

If you use the function to update an existing transient option that has not yet expired, the absence of the third parameter (expiration value) will keep the existing expiration time.

For example:

$initial = time();
set_transient( 'foo', 'bar', 300 );
sleep( 10 );
$update = time();
set_transient( 'foo', 'barbar' );

In this case the expiration time will remain as it was before $initial + 300 (and will not change to time() + 300, or will not be deleted), because if the $expiration parameter is not specified, the expiration time is not handled in any way and remains as it was.

Be careful, because you can unintentionally set the lifetime to never expires if it has expired and a second call is made without the $expiration parameter.

Changelog

Since 2.8.0 Introduced.

set_transient() code WP 6.5.2

function set_transient( $transient, $value, $expiration = 0 ) {

	$expiration = (int) $expiration;

	/**
	 * Filters a specific transient before its value is set.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 3.0.0
	 * @since 4.2.0 The `$expiration` parameter was added.
	 * @since 4.4.0 The `$transient` parameter was added.
	 *
	 * @param mixed  $value      New value of transient.
	 * @param int    $expiration Time until expiration in seconds.
	 * @param string $transient  Transient name.
	 */
	$value = apply_filters( "pre_set_transient_{$transient}", $value, $expiration, $transient );

	/**
	 * Filters the expiration for a transient before its value is set.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 4.4.0
	 *
	 * @param int    $expiration Time until expiration in seconds. Use 0 for no expiration.
	 * @param mixed  $value      New value of transient.
	 * @param string $transient  Transient name.
	 */
	$expiration = apply_filters( "expiration_of_transient_{$transient}", $expiration, $value, $transient );

	if ( wp_using_ext_object_cache() || wp_installing() ) {
		$result = wp_cache_set( $transient, $value, 'transient', $expiration );
	} else {
		$transient_timeout = '_transient_timeout_' . $transient;
		$transient_option  = '_transient_' . $transient;

		if ( false === get_option( $transient_option ) ) {
			$autoload = 'yes';
			if ( $expiration ) {
				$autoload = 'no';
				add_option( $transient_timeout, time() + $expiration, '', 'no' );
			}
			$result = add_option( $transient_option, $value, '', $autoload );
		} else {
			/*
			 * If expiration is requested, but the transient has no timeout option,
			 * delete, then re-create transient rather than update.
			 */
			$update = true;

			if ( $expiration ) {
				if ( false === get_option( $transient_timeout ) ) {
					delete_option( $transient_option );
					add_option( $transient_timeout, time() + $expiration, '', 'no' );
					$result = add_option( $transient_option, $value, '', 'no' );
					$update = false;
				} else {
					update_option( $transient_timeout, time() + $expiration );
				}
			}

			if ( $update ) {
				$result = update_option( $transient_option, $value );
			}
		}
	}

	if ( $result ) {

		/**
		 * Fires after the value for a specific transient has been set.
		 *
		 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
		 *
		 * @since 3.0.0
		 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
		 * @since 4.4.0 The `$transient` parameter was added.
		 *
		 * @param mixed  $value      Transient value.
		 * @param int    $expiration Time until expiration in seconds.
		 * @param string $transient  The name of the transient.
		 */
		do_action( "set_transient_{$transient}", $value, $expiration, $transient );

		/**
		 * Fires after the value for a transient has been set.
		 *
		 * @since 3.0.0
		 * @since 3.6.0 The `$value` and `$expiration` parameters were added.
		 *
		 * @param string $transient  The name of the transient.
		 * @param mixed  $value      Transient value.
		 * @param int    $expiration Time until expiration in seconds.
		 */
		do_action( 'setted_transient', $transient, $value, $expiration );
	}

	return $result;
}