Transients are not something that are on your radar when you first start developing in WordPress. However, you’ll soon start to hear about them and you’ll realise that you really need to consider using them. Hopefully for you, it’s sooner than it was for me, because I learned the hard way, by repeatedly bringing a site to its knees because of complex, repeated queries to the database.
Transients – what and why
So, what are transients then? In short, they are a way to temporarily store any data you want for quick retrieval. WordPress has an API that does all of the heavy lifting for you: all you need to do is tell it what information you want to store, give it a key name to make it easy to retrieve that information, and tell WordPress how long it should store that data before either deleting it, or refreshing it.
Why use them? Well, you can make some very complex queries to the WordPress database, and those queries take time. Instead of repeating them on every page load, the idea is to store that information in a cache so that it can be retrieved very easily and quickly. Think of it like this: if you have some files that you need to access on a daily basis, that are spread out all over your computer, instead of searching for them every morning, you can create a special folder with links to all of the files that you need, so that getting them is much faster. This is kind of how transients work.
A few notes about transients in WordPress:
- They are strictly for temporary data. Do not use transients if losing that data would be problematic to you.
- You specify how long the transient should be kept for, but understand that it is not guaranteed that the data will be deleted or refreshed after that exact amount of time. Instead, whenever a transient is fetched, it first checks the expiration time, and if it has passed, it deletes the transient (and re-caches it if your code is set up that way). So, the data is actually kept for your set expiration time + however long it takes for that transient to be requested again after the expiration time has passed.
- In my opinion, you should always set an expiration time: if you’re not setting an expiration time, the data is not transient and shouldn’t be stored as such.
The transient API functions
There are three main functions that you’ll need to know about when dealing with transients and they’re incredibly simple to use:
- set_transient(): this is the function that writes a transient to the database. It accepts three parameters: they key (name) and value (information), which are required, and an optional expiration time, in seconds.
- get_transient(): this function retrieves the transient data from the database. It only accepts one parameter: the transient key.
- delete_transient(): this function deletes a transient in the database so that it can be rebuilt, or removed permanently if it is no longer needed, rather than waiting for it to expire based on its expiration time (if it has one). It also accepts just one parameter: the transient key.
How to use transients
I have put together three really helpful tutorials to show you three different ways to use transients for very common WordPress functions, which you could probably very easily implement on your own site. Coming up over the next few days are the following tutorials to help you to understand the simplicity and power of transients, and make your sites much quicker in the process.