admin管理员组

文章数量:1335840

I am setting up a transient with one hour expiry time. Now i want to know how much time left for the transient to expire.

I am getting the transient timeout value with get_option function.

Can anyone help me out.

Thank You.

I am setting up a transient with one hour expiry time. Now i want to know how much time left for the transient to expire.

I am getting the transient timeout value with get_option function.

Can anyone help me out.

Thank You.

Share Improve this question asked Nov 21, 2016 at 7:00 chaitanyachaitanya 1111 silver badge2 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 7

The problem here is that if the site is actually using something like Redis, or any other supported "external cache" then nothing is going to actually be set in the options table (which can be checked with wp_using_ext_object_cache)

Storage in the options table is just how WordPress handles transients outside of any external caching (or local object caching).

With that said, to calculate the time remaining, you would do something like this:

$expires = (int) get_option( '_transient_timeout_MY_TRANSIENT_NAME', 0 );
$time_left = $expires - time();

The value for expiration is calculated by adding time() to the value passed when setting the transient, so to get time left, just subtract time() from the value set for expiration

There is no built-in WordPress function to get the transient timeout. But you can use the following function to get the transient timeout.

function get_transient_timeout( $transient ) {
    global $wpdb;
    $transient_timeout = $wpdb->get_col( "
      SELECT option_value
      FROM $wpdb->options
      WHERE option_name
      LIKE '%_transient_timeout_$transient%'
    " );
    return $transient_timeout[0];
}

Transients by definition can expire at any moment, no matter what interval you have requested, therefor the "time till expiry" can not be reliably determined. you can hack something by inspecting the "raw" option, but it is a bad idea to relay on it.

本文标签: phphow to get the value of time left for a transient