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
parameters | data type | Required or not | descriptive | default value |
---|---|---|---|---|
$transient | string (computer science) | be | Transient data name, which must not exceed 172 characters in length. | not have |
$value | multi- | be | Transient data values, if non-scalar, must be serializable. | not have |
$expiration | integer (math.) | clogged | Expiration time in seconds | 0 |
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.
1 thoughts on “set_transient 设置或更新瞬态数据的值”
Transient data? Cache it. Don't turn it over.