Faster WordPress Site With Transients

If you ever wanted to make your WordPress site faster then getting familiar with WordPress transients is the way to go. Some sites have reported speed-ups of up to 2x by simply changing their theme, plugins, or functions to use transients. But what are transients and how do you use them to get more speed to your site? Let’s find out…

What is the Transient API?

In short, the Transients API is very similar to the Options API but with the added feature of an expiration time, which simplifies the process of using the wp_options database table to temporarily store cached information.

Transients– WordPress.org Codex

How to use transients

So let’s see how a transient works. In short, it only has three states that are set_, get_, and delete_. Though you might not even need the delete if you don’t need to delete the data before the transient expires.

For this example, we will not be using the delete_ since we only need to set_ and get_ the transient and it can expire at its own time.

In the example, we first check if we have a transient called “foo_featured_posts“. If there is none we will run the WP_Query to get five featured posts. After the five featured posts have been received we save the results ($featured) in a new transient called “foo_featured_posts” and save it for 12 hours.

<?php
// Check for transient. If none, then execute WP_Query
if ( false === ( $featured = get_transient( 'foo_featured_posts' ) ) ) {
      $featured = new WP_Query(
	   array(
		'category' => 'featured',
		'posts_per_page' => 5
	   ));
	// Place the $featured value in transient. Expire after 12 hours.
	set_transient( 'foo_featured_posts', $featured, 12 * HOUR_IN_SECONDS );
} ?>

This means that from now on instead of running a WP_Query every single time we want to post five featured posts on our page, we can instead just call the transient “foo_featured_posts” to get the results.

Note – If the transient is not set it will return “false“. This is also why we are comparing the variable $featured to it to see if the transient value is set_ or not.

Why not use transients?

As fun and awesome transients are in theory there are a few things you should consider before using them.

Are you familiar with PHP?

This one is the most obvious one since you are required to understand a bit of coding (thankfully not much) to really understand the benefits of it and what it does. If you are not then, we recommend contacting a professional WordPress developer who can help you with transients and can consult you if you can or should use them on your site.

Check out WP Rocket for an easy way to speed up your site without the need to know how to code or learn PHP.

WP Rocket is much more than just a WordPress caching plugin. It’s the most powerful solution to boost your loading time, improve your PageSpeed score, and optimize your Core Web Vitals.

Caching is troublesome

Caching done right is awesome, but when done wrong it takes away from the user(s) who are visiting your site, and since transients are saving data (caching it) and then presenting it to the users again and again in a timed window. This is why they shouldn’t be used without clear thought on why and when they actually benefit the site and its user(s).

The data might or might not be saved at all

Something a lot of people might miss regarding transients is actually quite important when you read the documentation:

Transients should be used to store any data that is expected to expire, or which can expire at any time. Transients should also never be assumed to be in the database, since they may not be stored there at all.

Transients – WordPress.org Codex

… meaning you should never save anything that you actually want to keep, in a transient.

When to use transients?

Now that we know why you shouldn’t use transients let’s talk a bit about when to use them. If you really want to speed up your site then you could easily turn your theme parts into transients. For example, save the header in a transient or the body of your front page. These might be parts that don’t change that often thought might have some dynamic content like new blog posts that are only posted once a day or even once a week, meaning instead of loading the part with the blog posts and doing a query request to the database every time a visitor visits your frontpage, serve them a copy of the frontpage that already has saved the latest posts making it load much much faster.

Another use case would be saving some data you have fetched from an API that requires an update. For example in our earlier post “How To Fetch Data From An API In WordPress” where we fetched the price of the bitcoin, we could easily save this function in a transient to only run it for example just once an hour, so we wouldn’t need to make a request every time a page with that shortcode and function loads, making our pages with that shortcode and function load much much faster!

Conclusion

Getting familiar with the WordPress Transients API can be a bit tricky, but after learning it you can make your sites and plugins extremely fast by saving cached data in the WordPress database temporarily, thus speeding up the process of your site’s loading.

Note – To get the absolute best results with transients check out servers that use technologies like Redis that save the data in memcache instead of the database to make the loading even faster!

To learn more ways to speed up your site, read the “Ultimate Guide To WordPress Speed Optimization 2024“.

Similar Posts

Leave a Reply

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