How WordPress Transients Work

What is a transient?

A transient is a way of storing data in WordPress for temporary purposes – it’s often used for caching, for example. Transients can, optionally, be given an expiry timestamp, after which time they will be deleted.

How to use transients?

There are 3 functions – set_transient, get_transient and delete_transient. Well, actually there are 6, because the same commands also exist for multisite installations – set_site_transient, put_site_transient and delete_site_transient. The difference is that the latter will store transients that can be accessed site wide, rather than for a specific sub-site.

So, let’s say you have a multisite installation which consists of 2 sites – Site A and Site B. If you use set_transient in a plugin running on Site A then you can only access it from that site. If you use set_site_transient on Site A then Site B can also access it, but you’ll need to use get_site_transient to do so.

Parameter wise, in the case of get and delete, you simply need to specify the transient name. For set you need to also specify the value you wish to store and, optionally, an expiry (in seconds from now).

How are transients stored?

For a standalone installations, all transients are stored in the options table. They have a name of _transient_, followed by the transient name you specified. If you specified a timeout then that will be stored as a second record with the prefix of _transient_timeout_.

If you use get_site_transient, set_site_transient, etc., then those transients, too, will be stored on the same table. However, they are prefixed with _site_transient_ (and in the case of the timeout, as _site_transient_timeout_). Because they have different prefixes this you means you can use a the same name as a standard transient and both will be stored independently.

For multisite installations, it gets a bit more complicated. Using get_transient, set_transients_etc will cause the data to be stored on the options table for the relevant site (each site has its own options table). However, use of get_site_transient, set_site_transient, etc., will use the sitemeta table. In all cases the same prefixes are used as detailed above.

Housekeeping

When using get_transient or get_site_transient if an expiry record exists and it has expired then the transient will be deleted nothing will be returned. However, this is the only housekeeping that exists. If no attempt is made to access the record and it has expired then the record will remain (or rather two records, as there is one for the transient content and one for the timeout data).

Object Cache

The exception to all of the above is if you are using persistent object caching. In this case, the databases aren’t used and the data is stored directly to the cache. The same transient commands, however, are used and it’s simply the storage method that changes.

The author of this article has contributed to WordPress Core with transient housekeeping improvements and has written the Transient Cleaner plugin.

3 responses

  1. Very misleading naming conventions around these.

    I thought get_site_transient will work for an individual site in a multisite installation. Why isn’t it called get_network_transient?

    Either way, thanks so much for clearing up the confusion 🙂

  2. Thanks for this, it’s a helpful explanation.

    > If no attempt is made to access the record and it has expired then the record will remain

    I looked into this some more and it seems like about a year after this post was written that [a cron job was added to remove expired transients](https://github.com/WordPress/wordpress-develop/commit/8d56eff0733577910d6cb9b19b87d9b853d250e8) on a daily basis. So things should get cleaned up more regularly.

    1. This is something I really need to look into – the last I looked, some housekeeping was present but not everything that I’d expect.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from David Artiss

Subscribe now to keep reading and get access to the full archive.

Continue reading