05 Sep

WordPress Transients – Storing False

Storing information in transients is a great way to speed up WordPress, especially if you’re using a memory cache instead of the options table. However, storing false in transients is difficult, because false means there is no transient in the cache. So how do you store false in WordPress transients?

For a great introduction to transients, see https://css-tricks.com/the-deal-with-wordpress-transients/

Why would you want to store false? Generally you would want to store false when it is a legitimate value – for example, when looking for the permalink for a slug, if the slug doesn’t exist then false is a valid return, and you wouldn’t want to query the database every time you looked up the missing slug.

So, what is the best way to store the value false in the WordPress transient system? Replace it with some value that will never normally be a valid response:


function MyTestFunction(){
    $TransientKey = 'myprefix_mytransientkey';
    $return = get_transient($TransientKey);
    if ( $return === false) {
        $return = 123; // find the value and set it.
        if ( $return === false ){
            $return = TRANSIENT_VALID_FALSE ;
        set_transient($TransientKey, $return, HOUR_IN_SECONDS) ;
    if ( $return == TRANSIENT_VALID_FALSE ){
        $return = false ;
    return $return ;

Leave a Reply

Your email address will not be published. Required fields are marked *