WordPress Logo

WordPress Transients, a Simple Example

The first time I heard of WordPress Transients, it sounded scary. But after figuring out what they were, how to create them and what they were used for, it is a no-brainer to not use them in certain situations. According to the WordPress codex:

WordPress Transients API offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted.”

“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. “

“Advantage: Transients expire and Transients are inherently sped up by caching plugins, where normal Options are not.”

I build a lot of custom WordPress websites which uses custom database tables and I have to create custom database queries. This is a good option to use Transients API especially with database queries with multiple JOINS. Using transients will speed up the page loading since most likely a Transient will already be present with the results compared to making a database call each time the page is loaded.  According to the WordPress codex, “Using transients in your plugins and themes is simple and only adds a few extra lines of code, but if used in the right situations (long/expensive database queries or complex processed data) it can save seconds off the load times on your site.

There are three basic functions to use Transients, create or set, get and delete.

How do we create a transient.

set_transient( 'special_query_results', $special_query_results, 12 * HOUR_IN_SECONDS );

How do we get a transient.

get_transient( 'special_query_results' );

How do we delete a transient.

delete_transient( $transient );


Let’s take a look at a real world example. When working with Transients we want to always to a check to see if the Transient you are looking for exist and if not then you would create it as it is possible for the transient to not be available before the expiration time. For example, say you have a brand new page that does not get a lot of traffic and the code to create the transient has not run yet and then your code is looking for a transient on another page that is not there. Also, what if your Transient expired, you always need to run a check to if it exists first and if not then create it. Then the next time the page loads, the Transient should be available.

$routes = get_transient( 'cached_shipping_routes' );
if ( empty( $routes ) ) {
    global $wpdb;
    $routes_query = $wpdb->get_results("SELECT * FROM `shipping_routes` WHERE route_id  NOT IN  ('COLLEGE', 'AIRPORT', 'HOSPITAL', 'CIVIC CENTER', 'MEDICAL CLINIC')");
    set_transient( 'cached_shipping_routes', $routes_query, 6 * MONTH_IN_SECONDS );
if ( $routes ) {
    echo '<select id="shipping_route_ddl" name="shipping_route_ddl" class="shipping_select_ddl" aria-label="Select A Route"><option value="select">SELECT A ROUTE</option>';
    foreach ( $routes as $route_value ) {
         echo '<option value="' . esc_attr( $route_value->route_id ) . '">' . esc_attr( $route_value->route_short_name ) . ' / ' . esc_attr( $route_value->route_long_name ) . '</option>';
    echo '</select>';

Let’s break down the above code:

Line 1: we attempt to get the transient titled ‘cached_shipping_routes’ and set it to a variable called routes.

Line 2-5 we do a check to see if the $routes variable is empty. if it is empty then this transient is not available thus we are going to run our database query and then set the results to a variable called $routes_query. We then create the Transient using set_transient.  We pass in the title of the Transient, then the $routes_query variable which holds the results from our database query and lastly we set the Expiration time.

If the $routes variable is NOT EMPTY, then the Transient we are looking for is there, and then we use the $routes variable which holds our Transient value from Line 1 and we create our dynamically generated select element.

Now this query is not large query with multiple JOINS but the database table is large with a lot of rows and we are excluding certain rows with specific route ids. This allows our page to load faster. Of course the first time the Transient is NOT available, the page will load slower this time but after that the page will load faster, especially if you use some type of caching.

Here is a reference from to the WordPress Codex on Transient.  https://codex.wordpress.org/Transients_API

There is also an excellent plugin from Pippen Williamson called Transients Manager which allows to view all of your websites Transient from the admin.


Blog, WordPress,