get_transient()获取某个保留在数据库的临时值

目录

描述

Get the value of a transient.

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

get_transient() 描述

用法

<?php get_transient$transient ); ?>

get_transient() 用法

参数

$transient

(string) (必填) Unique transient name (this should be 45 characters or less in length. NOTE: if using a site transient, it should be 40 characters or less in length). Expected to not be SQL-escaped.

默认值: None

get_transient() 参数

返回值

(mixed) 

Value of transient. If the transient does not exist, does not have a value, or has expired, then get_transient will return false. This should be checked using the identity operator ( === ) instead of the normal equality operator, because an integer value of zero (or other "empty" data) could be the data you're wanting to store. Because of this "false" value, transients should not be used to hold plain boolean values. Put them into an array or convert them to integers instead.

get_transient() 返回值

示例

Example of using get_transient, set_transient and WP_Query

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
// 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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?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 );
}

?>

get_transient() 示例

历史

添加于 版本: 2.8

get_transient() 历史

源文件

get_transient() 函数的代码位于 wp-includes/option.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Get the value of a transient.
 *
 * If the transient does not exist, does not have a value, or has expired,
 * then the return value will be false.
 *
 * @since 2.8.0
 *
 * @param string $transient Transient name. Expected to not be SQL-escaped.
 * @return mixed Value of transient.
 */
function get_transient( $transient ) {

 	/**
	 * Filter 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
	 *
	 * @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.
	 */
	$pre = apply_filters( 'pre_transient_' . $transient, false );
	if ( false !== $pre )
		return $pre;

	if ( wp_using_ext_object_cache() ) {
		$value = wp_cache_get( $transient, 'transient' );
	} else {
		$transient_option = '_transient_' . $transient;
		if ( ! defined( '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="" );="" }="" *="" *="" filter="" an="" existing="" transient's="" value.="" *="" *="" the="" dynamic="" portion="" of="" the="" hook="" name,="" `$transient`,="" refers="" to="" the="" transient="" name.="" *="" *="" @since="" 2.8.0="" *="" *="" @param="" mixed="" $value="" value="" of="" transient.="" */="" return="" apply_filters(="" 'transient_'="" .="" $transient,="" $value="" );="" }="">

get_transient() 源文件

相关