wppaste
WordPress

get_site_transient( string $transient ): mixed

Since
2.9.0
Source
wp-includes/option.php:2564

Fetches a network-wide cached value stored under a transient key, returning false when it is missing, empty, or expired. On single-site installs it behaves the same as a regular site option lookup, since the underlying get_site_option() falls back to the options table there. Because a legitimately stored false and a missing transient look identical, pair it with set_site_transient() and a sentinel value if you need to distinguish the two. The pre_site_transient_{$transient} filter can also short-circuit the whole lookup and return something else entirely.

Retrieves the value of a site transient.

Description

If the transient does not exist, does not have a value, or has expired, then the return value will be false.

Compatibility

WordPress
since 2.9.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$transientstring
Transient name. Expected to not be SQL-escaped.

Return value

mixed
Value of transient.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Check whether WordPress has cached available plugin updates

Core stores the results of its update check in a site transient named update_plugins, so this reads it back without triggering a new check.

$updates = get_site_transient( 'update_plugins' );

if ( false === $updates ) {
	echo esc_html( 'No cached plugin update data yet.' );
} else {
	echo '<pre>' . esc_html( print_r( $updates, true ) ) . '</pre>';
}

On a fresh install this transient may not be set yet, so the false branch is the expected first result.

Cache an expensive site-wide calculation across all posts

Rather than recounting every post on each page load, this stores the count in a site transient for ten minutes and reads it back through get_site_transient().

$cache_key = 'my_plugin_published_post_count';
$count = get_site_transient( $cache_key );

if ( false === $count ) {
	$count = wp_count_posts()->publish;
	set_site_transient( $cache_key, $count, 10 * MINUTE_IN_SECONDS );
	echo esc_html( 'Calculated fresh count: ' . $count );
} else {
	echo esc_html( 'Read cached count: ' . $count );
}

Run the snippet twice in the sandbox to see the second run hit the cached branch instead of recalculating.

Common problems and fixes · 3

Why does get_site_transient() return false even right after I called set_site_transient() with the same key?

The function checks the pre_site_transient_{$transient} filter before it ever looks at stored data, and any callback there returning a non-false value short-circuits the whole lookup. If nothing hooks that filter, the more common cause is a persistent object cache dropping the value or wp_using_ext_object_cache() routing the read to a cache group that was never populated.

How do I know if the transient truly doesn't exist versus it was intentionally stored as false?

Store a wrapping value instead of raw false:
  • Save an array like array( 'value' => false ) with set_site_transient()
  • Or save a unique string sentinel and compare against it after retrieval
  • Or track existence separately with a boolean site option

Why doesn't my update_plugins or update_themes site transient ever seem to expire?

The source hardcodes update_core, update_plugins, and update_themes into a no_timeout array and skips the expiration check entirely for those three keys, so they persist until something explicitly deletes or overwrites them.

Alternatives and related functions

get_transient
When the cached value only needs to apply to the current site rather than the whole network.
set_site_transient
When you need to store or refresh the value that get_site_transient() will later read back.
delete_site_transient
When you need to invalidate a cached value immediately instead of waiting for its expiration, especially for the update_core, update_plugins, and update_themes keys that never time out.
get_site_option
When you need persistent site-wide data with no expiration at all, rather than a value meant to go stale.

Performance profile

How much work a call to get_site_transient() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_site_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
11–55

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 62.

Plugin surface
2 hooks

Third-party callbacks on 'pre_site_transient_{$transient}', 'site_transient_{$transient}' run inside this call, and their cost is not bounded by anything here.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • transienttransientget_site_transient()this function does it
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()called directly
  • optionnetwork optionget_site_option()called directly

Further down the call graph this can also reach serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_site_transient() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$pre !== false11apply_filters()
$pre === false && wp_using_ext_object_cache()26apply_filters(), wp_using_ext_object_cache(), wp_cache_get(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && $transient && isset($value)28apply_filters(), wp_using_ext_object_cache(), wp_installing(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && wp_installing()29apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_cache_get(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing()32–40apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !$transient && $timeout === false && !isset($value)44apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), get_site_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !$transient && $timeout !== false && !$timeout && isset($value)44apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), time(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !$transient && $timeout !== false && !$timeout && !isset($value)48apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), time(), get_site_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !$transient && $timeout !== false && $timeout && isset($value)51apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), time(), delete_site_option(), delete_site_option(), apply_filters()
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && !$transient && $timeout !== false && $timeout && !isset($value)55apply_filters(), wp_using_ext_object_cache(), wp_installing(), get_site_option(), time(), delete_site_option(), delete_site_option(), get_site_option(), apply_filters()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 62 instructions, 11–55 executed per call, 7 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while get_site_transient() runs, in this order:

  1. apply_filters( pre_site_transient_{$transient} )filterline 2582 (+18 into the body)

    Filters the value of an existing site transient before it is retrieved.

  2. apply_filters( site_transient_{$transient} )filterline 2622 (+58 into the body)

    Filters the value of an existing site transient.

Uses · 7

Used by · 50

Show all 50

Source code

function get_site_transient( $transient ) { 	/**	 * Filters the value of an existing site transient before it is retrieved.	 *	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.	 *	 * Returning a value other than boolean false will short-circuit retrieval and	 * return that value instead.	 *	 * @since 2.9.0	 * @since 4.4.0 The `$transient` parameter was added.	 *	 * @param mixed  $pre_site_transient The default value to return if the site 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_site_transient_{$transient}", false, $transient ); 	if ( false !== $pre ) {		return $pre;	} 	if ( wp_using_ext_object_cache() || wp_installing() ) {		$value = wp_cache_get( $transient, 'site-transient' );	} else {		// Core transients that do not have a timeout. Listed here so querying timeouts can be avoided.		$no_timeout       = array( 'update_core', 'update_plugins', 'update_themes' );		$transient_option = '_site_transient_' . $transient;		if ( ! in_array( $transient, $no_timeout, true ) ) {			$transient_timeout = '_site_transient_timeout_' . $transient;			wp_prime_site_option_caches( array( $transient_option, $transient_timeout ) ); 			$timeout = get_site_option( $transient_timeout );			if ( false !== $timeout && $timeout < time() ) {				delete_site_option( $transient_option );				delete_site_option( $transient_timeout );				$value = false;			}		} 		if ( ! isset( $value ) ) {			$value = get_site_option( $transient_option );		}	} 	/**	 * Filters the value of an existing site transient.	 *	 * The dynamic portion of the hook name, `$transient`, refers to the transient name.	 *	 * @since 2.9.0	 * @since 4.4.0 The `$transient` parameter was added.	 *	 * @param mixed  $value     Value of site transient.	 * @param string $transient Transient name.	 */	return apply_filters( "site_transient_{$transient}", $value, $transient );}

Changelog

Introduced in 2.9.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/option.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.