I Doubled my Income in a Year

WordPress allowed me to get a better job & better projects in just a Year. I will give you tips, tricks and guide you into WordPress development so you can get more income. Sign up to my 7-day email course!

Parrot with Article Text

Should You Use WordPress Transients?

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:)

About the Author 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.

follow me on:

EDD for Developers

In less than 2 weeks, I have earned $1,800 by working on a custom solution with Easy Digital Downloads without any prior experience. Let me teach you how you can too with various custom solutions.

Get the eBook

Leave a Comment:

2 comments
Damien says March 13, 2017

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
Add Your Reply

Should You Use WordPress Transients?

by Igor Benic time to read: 2 min
2
Learn how to develop with Easy Digital DownloadsGet the eBook
+