set_transient Set or update the value of transient data

Posted in:
Update time:2021-08-22

Setting or updating transient data values in WordPress is often used to save data that doesn't need to be there all the time.

function parameter

parametersdata typeRequired or notdescriptivedefault value
$transientstring (computer science)beTransient data name, which must not exceed 172 characters in length.not have
$valuemulti-beTransient data values, if non-scalar, must be serializable.not have
$expirationinteger (math.)cloggedExpiration time in seconds0

Function Return Value

Returns true if the setting is successful, otherwise returns false

usage example

In the following example, we get the 5 most recent posts, saved in a file named latest_5_posts, with an expiration date of 1 day.

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
    'orderby' => 'date', 'order' => 'DESC', 'order' => 'DESC'
    'order' => 'DESC'
).
$latest_post = new WP_Query( $args );

// Save the results to a transient named latest_5_posts
set_transient( 'latest_5_posts', $latest_post, DAY_IN_SECONDS );

More information

For the parameter $transient should be less than or equal to 172 characters if memcached is not enabled, because WordPress prefixes this variable with "_transient_" or "_transient_timeout_" (depending on whether it's expired or not) in the Options datasheet, and if the name is too long, the data will default to an active state. transient_timeout_" in the options data table (depending on whether it expires or not), and if the name is too long, the data will be in the live state by default.

If the transient data already exists, the change function updates the transient data to the expiration time.

Note: If the transient data does not have an expiration time set, the data will be automatically loaded. Take this into account when adding transient data that will not be used on every page, so as not to image page performance by automatically loading too many unnecessary transients.

related function

get_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.

1 thoughts on “set_transient 设置或更新瞬态数据的值

发表回复

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

*