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.
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?
- How do I know if the transient truly doesn't exist versus it was intentionally stored as false?
- Why doesn't my update_plugins or update_themes site transient ever seem to expire?
Why does get_site_transient() return false even right after I called set_site_transient() with the same key?
How do I know if the transient truly doesn't exist versus it was intentionally stored as 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?
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
- Scaling
- Constant
- Instructions
- 11–55
- Plugin surface
- 2 hooks
- Called by
- 50
Reads stored settings via get_site_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 62.
Third-party callbacks on 'pre_site_transient_{$transient}', 'site_transient_{$transient}' run inside this call, and their cost is not bounded by anything here.
50 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- transienttransient
get_site_transient()this function does it - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()called directly - optionnetwork option
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
$pre !== false | 11 | apply_filters() |
$pre === false && wp_using_ext_object_cache() | 26 | apply_filters(), wp_using_ext_object_cache(), wp_cache_get(), apply_filters() |
$pre === false && !wp_using_ext_object_cache() && !wp_installing() && $transient && isset($value) | 28 | apply_filters(), wp_using_ext_object_cache(), wp_installing(), apply_filters() |
$pre === false && !wp_using_ext_object_cache() && wp_installing() | 29 | apply_filters(), wp_using_ext_object_cache(), wp_installing(), wp_cache_get(), apply_filters() |
$pre === false && !wp_using_ext_object_cache() && !wp_installing() | 32–40 | apply_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) | 44 | apply_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) | 44 | apply_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) | 48 | apply_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) | 51 | apply_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) | 55 | apply_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:
- apply_filters( pre_site_transient_{$transient} )filterline 2582 (+18 into the body)
Filters the value of an existing site transient before it is retrieved.
- apply_filters( site_transient_{$transient} )filterline 2622 (+58 into the body)
Filters the value of an existing site transient.
Uses · 7
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_using_ext_object_cache()Toggles `$_wp_using_ext_object_cache` on and off without directly touching global.
- wp_installing()Checks or sets whether WordPress is in "installation" mode.
- wp_cache_get()Retrieves the cache contents from the cache by key and group.
- wp_prime_site_option_caches()Primes specific network options for the current network into the cache with a single database query.
- get_site_option()Retrieve an option value for the current network based on name of option.
- delete_site_option()Removes an option by name for the current network.
Used by · 50
- Plugin_Upgrader::bulk_upgrade()Upgrades several plugins at once.
- Plugin_Upgrader::upgrade()Upgrades a plugin.
- Theme_Upgrader::bulk_upgrade()Upgrades several themes at once.
- Theme_Upgrader::upgrade()Upgrades a theme.
- WP_Automatic_Updater::run()Kicks off the background update process, looping through all pending updates.
- WP_Community_Events::get_cached_events()Gets cached events.
- WP_Debug_Data::get_wp_active_theme()Gets the WordPress active theme section of the debug data.
- WP_Debug_Data::get_wp_parent_theme()Gets the WordPress parent theme section of the debug data.
- WP_Debug_Data::get_wp_plugins_raw_data()Gets the raw plugin data for the WordPress active and inactive sections of the debug data.
- WP_Debug_Data::get_wp_themes_inactive()Gets the WordPress inactive themes section of the debug data.
- WP_Feed_Cache_Transient::load()Retrieves the data saved in the transient.
- WP_Feed_Cache_Transient::mtime()Gets mod transient.
Show all 50
- WP_Font_Collection::load_from_url()Loads the font collection data from a JSON file URL.
- WP_MS_Themes_List_Table::column_autoupdates()Handles the auto-updates column output.
- WP_MS_Themes_List_Table::prepare_items()Prepares the themes list for display.
- WP_Plugin_Dependencies::get_dependency_api_data()Retrieves and stores dependency plugin data from the WordPress.org Plugin API.
- WP_Plugin_Install_List_Table::get_installed_plugins()Returns the list of known plugins.
- WP_Plugins_List_Table::prepare_items()Prepares the list of items for displaying.
- WP_REST_Pattern_Directory_Controller::get_items()Search and retrieve block patterns metadata
- WP_REST_URL_Details_Controller::get_cache()Utility function to retrieve a value from the cache at a given key.
- WP_Theme::get_pattern_cache()Gets block pattern cache.
- _maybe_update_core()Determines whether core should be updated.
- _maybe_update_plugins()Checks the last time plugins were run before checking plugin versions.
- _maybe_update_themes()Checks themes versions only after a duration of time.
- delete_plugins()Removes directory and files of a plugin for a list of plugins.
- find_core_auto_update()Gets the best available (and enabled) Auto-Update for WordPress core.
- find_core_update()Finds the available update for WordPress core.
- get_core_updates()Gets available core updates.
- get_plugin_updates()Retrieves plugins with updates available.
- get_theme_feature_list()Retrieves list of WordPress theme features (aka theme tags).
- get_theme_roots()Retrieves theme roots.
- get_theme_update_available()Retrieves the update link if there is a theme update available.
- get_theme_updates()Retrieves themes with updates available.
- install_plugin_install_status()Determines the status we can perform on a plugin.
- install_popular_tags()Retrieves popular WordPress plugin tags.
- search_theme_directories()Searches all registered theme directories for complete and valid themes.
- wp_ajax_update_theme()Handles updating a theme via AJAX.
- wp_check_browser_version()Checks if the user needs a browser update.
- wp_check_php_version()Checks if the user needs to update PHP.
- wp_credits()Retrieves the contributor credits.
- wp_get_available_translations()Get available translations from the WordPress.org API.
- wp_get_popular_importers()Returns a list from WordPress.org of popular importer plugins.
- wp_get_translation_updates()Retrieves a list of all language updates available.
- wp_get_update_data()Collects counts and UI strings for available updates.
- wp_plugin_update_row()Displays update information for a plugin.
- wp_plugin_update_rows()Adds a callback to display update information for plugins with updates available.
- wp_prepare_themes_for_js()Prepares themes for JavaScript.
- wp_theme_update_row()Displays update information for a theme.
- wp_theme_update_rows()Adds a callback to display update information for themes with updates available.
- wp_update_plugins()Checks for available updates to plugins based on the latest versions hosted on WordPress.org.
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.
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.