WordPress Transients are a great way to store temporary data. The data is stored in the database and it can be read from the memory with a plugin that uses WordPress cache correctly. But, should you use them always?

There was an article about the WordPress table options and how it should be maintained for a better performance. It was written by 10up, a company of people highly involved in the WordPress community.

One of the issues that were seen in slow options table are the WordPress transients, actually the abandoned transients. I was thinking about that and how to handle it. So I have decided to write this simple article on when should you use WordPress transients and also how to use them.

Before using WordPress Transients, you need to have a complete strategy for them and how to use them.

When to use WordPress Transients?

Since the WordPress Transient API provides you with some simple functions, you could get used to it and use them for anything. WordPress Queries are actually pretty fast, especially the posts table. They are designed to perform pretty fast on a large number of data.

Should I save WP_Queries inside WordPress Transients? No. You should not. That object is pretty complex and it could be a security risk. You should just construct an array of the data you need and save that instead.

But when? If you have a part of the page where you will need to perform pretty complex queries or perform more than one query on the database, then I would advise you to use the WordPress transients. Again, construct an array with the data you need and save that instead of the queries.

Don’t cache WordPress Queries with WordPress Transients. Cache an Array instead! (Click to Tweet)

You can also cache HTML! I have read about that somewhere and before it, I never thought about it. But, yeah, you can even cache a complete HTML and output it when that data is requested.

Control Your Transients!

I am not joking on this. You really should control them. If you have read the mentioned article from 10up (and if not you should), you will know that abandoned transients have been one of the reasons for a slow options table.

WordPress does erase the transients, but WordPress is built by many developers and it is hosted on various servers, with various plugins. There could be a bug in the code (anywhere). Don’t just trust WordPress and all the plugins, server setups.

I know WordPress is awesome, but we should be awesome ever more as developers. People do trust us and some of them don’t even think twice about installing a plugin.

What if a transient does get abandoned? What if they filled the database and the site runs slow? People will blame WordPress, people will blame developers. We don’t want that.

Delete Your Transients on Deactivation

Even if plugins could be deleted through FTP and we can’t do anything on it, we can delete our transients on deactivation or activation. It is as simple as calling a function.

function delete_your_transients_on_deactivation() {
   delete_transient( 'your_transient_name' );
}
register_deactivation_hook( __FILE__, 'delete_your_transients_on_deactivation' );

Your thoughts on Transient Usage?

Feel free and comment about your usages and what you think about transients. Every comment, info, tip, trick or question can help us with transients and help us to code the best WordPress sites possible:)

Become a Sponsor

Posted by Igor Benic

Web Developer who mainly uses WordPress for projects. Working on various project through Codeable & Toptal. Author of several ebooks at https://leanpub.com/u/igorbenic.

4 Comments

  1. I used transients on a client site but quickly changed to use postmeta because I was concerned that the the transients would be deleted after they expired and the code to generate the transient was breaking the site. I simply added the data to post ID 1 (a post I knew did not exist) and used a standalone script (run hourly by real cron) to update the data.

    It was good to read, in the 10up article, that the postmeta table is optimized.

    Reply

    1. Hi Damien, interesting approach! Postmeta is another good option for saving such data. You can check also the alternatives here: https://pressjitsu.com/blog/transient-cache-alternatives/

      Reply

  2. Thanks for article. When you say ‘construct an array with the data you need and save that instead of the queries.’ how do you do that without a query? For example, if I want to store post ids for a certain tax that gets updated with time, what method other than wp query?

    Reply

    1. You get the data from wp query, but then parse the data and construct an array out of it.

      So you don’t store the results from WP_Query (as it could store more data than you want).

      Reply

Leave a reply

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

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