The important role of transients in WordPress

WordPress includes a feature called transients, which acts as a caching mechanism. This allows you to keep data temporarily, with a set expiration time.

Transients help improve your site’s speed by saving information that would otherwise have to be retrieved multiple times from external sources.
For example, social media share counts, API responses, or database queries are frequently stored as transients to enhance performance and reduce the load on your server.

Example of media share counts in a website.
Media share counts are frequently stored as transients.

How to set, get, and delete transients

When you’re coding your own transients, there are three fundamental operations that you’ll probably need to implement.

Set transients

Setting transients means defining the key, value, and expiration period. To do this, follow a specific format.
set_transient( $transientName, $value, $expirationDate );

For example, here the transient is set in functions.php inside the contact form handler via a small helper function.

This helper saves a short-lived status message (success or error) in a transient so it can be shown to the user after the form handler redirects.

After sending the email and once wp_mail() runs, the helper is called with either ‘success’ or ‘error’ depending on whether the email was delivered.

The helper wraps the call and stores the transient with a 30-second TTL — just long enough to survive the redirect and be read on the next page load, then deleted.

Internally, WordPress stores transients in the wp_options table by default, but if an object cache (like Redis or Memcached) is active, transients are stored there instead, making them significantly faster.

Fetching transients

get_transient() is a WordPress function used to retrieve a value that was previously stored using set_transient().

The function takes a single argument: a string key that identifies the cached value.

get_transient( string $transient )
If the transient is missing, has no value, or has expired, the return value will be false.

Deleting transients

To delete a transient, simply use the delete_transient() function. You just need the key for the transient you want to remove. Keep in mind that you’ll need to do this for each transient you intend to delete.

delete_transient(string $transient);

A practical example

Practical example of using a transient in a contact-form message function.
this function attempts to retrieve a previously stored transient under the key contact_msg.

The function begins by attempting to retrieve a previously stored transient under the key contact_msg. If no transient exists — meaning no form submission has taken place, or the transient has already expired — get_transient returns false, and the function exits immediately with nothing rendered.

A crucial point in this example is that false is a legitimate value to keep in a transient. If your data can genuinely be false, it’s essential to wrap it (like in an array) before storing it. This way, you can tell the difference between a cache miss and a stored false value.

The second part of that guard clause, ! isset( $msg[‘type’], $msg[‘text’] ), validates the structure of the data. Since the transient is expected to be an associative array with both a ‘type’ and a ‘text’ key, this check ensures the data is well-formed before the function proceeds. This protects against corrupted or unexpected transient data.

Next, the function checks the type of the message. Instead of simply trusting the value in $msg[‘type’], it compares it against the allowed values of [‘success’, ‘error’] using in_array with strict mode set to true. If the type doesn’t match either of these allowed values, it defaults to ‘error’. This helps to prevent any unintended CSS classes from being added to the output.

Then it assigns a CSS class name to $class based on the value of $type. It uses PHP’s ternary operator.

The condition $type === 'success' uses strict comparison, so both value and type must match exactly. If true, $class becomes 'contact-success'; otherwise, it becomes 'contact-error'.

In practice, this means only the exact string ‘success‘ is treated as a success state. Any other value (such as ‘error‘, null, or ‘Success‘) will fall back to ‘contact-error‘.

The delete_transient() function is deliberately called after all the data has been read and set up, but right before the HTML is rendered. This order is important—if the deletion happened earlier and something went wrong during the function, you would lose the message without it ever being shown. The best time to delete is just before the output.

What to bear in mind

Once transients expire, they are removed from your database. Therefore, it’s a good idea to use them for information that you frequently recreate.
While they can enhance your project’s performance, transients are most effective for handling large queries. If the work involved in creating a transient is more than simply making a new request for the resource, it’s smarter to avoid using them.

Although my blog doesn’t support comments, feel free to reply via email or X.