get_transient Get transient data

Posted in:
Update time:2021-08-23

该函数用来获取瞬态数据的值,如果瞬态数据不存在、没有值或已过期,则返回值将为false。

function parameter

parametersdata typeRequired or notdescriptivedefault value
$transientstring (computer science)be瞬态数据名称,预计不会被 SQL 转义not have

Function Return Value

如果获取成功,返回瞬态数据的值,如果值不存在或已过期,返回false。

usage example

下面的示例中,我们先尝试获取瞬态数据 special_query_results 的值,如果不存在,我们新建了一个 WP_Query,然后设置改瞬态缓存。

if ( false === ( get_transient( 'special_query_results' ) ) ) {
    $results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' );
    set_transient( 'special_query_results', $results );
}

related function

set_transient

We offer WordPress Themes and Plugins Custom Development Services

This site has long undertaken WordPress themes, plugins, WooCommerce-based store mall development business. We have 10 years of experience in WordPress development, if you want to Developing Websites with WordPress, please contact WeChat: iwillhappy1314 or email: amos@wpcio.com for inquiries.

发表回复

Your email address will not be published. 必填项已用 * 标注

*