get_transient( string $transient )
Get the value of a transient.
Description Description
If the transient does not exist, does not have a value, or has expired, then the return value will be false.
Parameters Parameters
- $transient
-
(string) (Required) Transient name. Expected to not be SQL-escaped.
Return Return
(mixed) Value of transient.
Source Source
File: wp-includes/option.php
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 | function get_transient( $transient ) { /** * Filters the value of an existing transient. * * The dynamic portion of the hook name, `$transient`, refers to the transient name. * * Passing a truthy value to the filter will effectively short-circuit retrieval * of the transient, returning the passed 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 the returned 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() ) { $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 ); } |
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Example of using
get_transient
,set_transient
andWP_Query
// Get any existing copy of our transient data
if
( false === (
$special_query_results
= get_transient(
'special_query_results'
) ) ) {
// It wasn't there, so regenerate the data and save the transient
$special_query_results
=
new
WP_Query(
'cat=5&order=random&tag=tech&post_meta_key=thumbnail'
);
set_transient(
'special_query_results'
,
$special_query_results
);
}
// Use the data like you would have normally...
Add
WP_DEBUG
in the conditional statement if you always want live data during development stage<?php
if
( WP_DEBUG
or
false === (
$special_query_results
= get_transient(
'special_query_results'
) ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$special_query_results
=
new
WP_Query(
'cat=5&order=random&tag=tech&post_meta_key=thumbnail'
);
set_transient(
'special_query_results'
,
$special_query_results
);
}
?>
Useful to know: If the transient exists but has expired, the return value will not just be false. The expired transient will be deleted at the same time.
WordPress does not delete expired transients on its own, but using get_transient() on an expired one will do so. Non-expired transients, or transients without expiration will only be deleted with delete_transient().