get_transient()WP 2.8.0

Get the value of a transient (a temporary option).

If a website is using object cache, then transient will be retrieved from the existing cache, not from the wp_options table directly.

On multisite, transients will be retrieved from the options of the current site/blog.

Use set_site_transient or get_site_transient to set or get a transient option of the main site.

Return

Mixed.

  • false — when the transient:

    • does not exist
    • does not have a value
    • has expired
  • Value of transient otherwise.

Note: the returned false should be checked with the identity equality operator (===), and not with the usual double equality, because the value of the option can contain 0, an empty string or an empty array. For the same reason, you do not need to save false into the option value, save 0 instead, or put false in an array.

Usage

get_transient( $transient );
$transient(string) (required)
Transient name. Expected to not be SQL-escaped.

Examples

0

#1 Saving a current query to the transient

This example shows how to use get_transient(), set_transient() with WP_Query to save the current query to the transient.

This may come in handy to save heavyweight queries.

// Get the transient
$special_query_results = get_transient( 'special_query_results' );

if ( false === $special_query_results ) {

	// The transient is empty so let's add it
	$special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' );
	set_transient( 'special_query_results', $special_query_results );
}

// Using $special_query_results as usual...

Changelog

Since 2.8.0 Introduced.

get_transient() code WP 6.5.2

function get_transient( $transient ) {

	/**
	 * Filters the value of an existing transient before it is retrieved.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * Returning a value other than false from the filter will short-circuit retrieval
	 * and return that value instead.
	 *
	 * @since 2.8.0
	 * @since 4.4.0 The `$transient` parameter was added
	 *
	 * @param mixed  $pre_transient The default value to return if the transient does not exist.
	 *                              Any value other than false will short-circuit the retrieval
	 *                              of the transient, and return that value.
	 * @param string $transient     Transient name.
	 */
	$pre = apply_filters( "pre_transient_{$transient}", false, $transient );

	if ( false !== $pre ) {
		return $pre;
	}

	if ( wp_using_ext_object_cache() || wp_installing() ) {
		$value = wp_cache_get( $transient, 'transient' );
	} else {
		$transient_option = '_transient_' . $transient;
		if ( ! wp_installing() ) {
			// If option is not in alloptions, it is not autoloaded and thus has a timeout.
			$alloptions = wp_load_alloptions();

			if ( ! isset( $alloptions[ $transient_option ] ) ) {
				$transient_timeout = '_transient_timeout_' . $transient;
				$timeout           = get_option( $transient_timeout );
				if ( false !== $timeout && $timeout < time() ) {
					delete_option( $transient_option );
					delete_option( $transient_timeout );
					$value = false;
				}
			}
		}

		if ( ! isset( $value ) ) {
			$value = get_option( $transient_option );
		}
	}

	/**
	 * Filters an existing transient's value.
	 *
	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.
	 *
	 * @since 2.8.0
	 * @since 4.4.0 The `$transient` parameter was added
	 *
	 * @param mixed  $value     Value of transient.
	 * @param string $transient Transient name.
	 */
	return apply_filters( "transient_{$transient}", $value, $transient );
}