Back to Top

How to use Transient API in WordPress

Introduction to Transient API in WordPress

Nowadays Caching is very important for any website development and WordPress provides own API for caching a data which is known as Transients API.Transients storage API is useful when you are using complex queries in your themes and plugins in WordPress. So Today we’ll talk about how you can use Transients APIs in WordPress itself.

What is Transients API?

Web page data caching is a way of storing data to local so every request for same data is not going to execute PHP and MySQL queries and also speed up the loading of the page AND reduce server load.The Transients API in WordPress is a very useful method for temporarily storing cached data in the database. Transients cache make your WordPress website faster and improve performance of the site.

Transients are temporary means that it has an expiration time and cached data will be deleted after a specified amount of time.

When to use Transients API?

  • Complex or custom database queries.
  • Static data or information like WordPress navigation menus.
  • Transients API is useful to fetch data from external sources like Facebook, Twitter or RSS feeds.

WordPress provides functions to work with Transients.Here I am going to explain about the most used three functions for transient.

get_transient() – To retrieve the cached data
set_transient() – To store data in a cache
delete_transient() – To delete cached data

You can store any data into transient liek array,textobject etc because it is storing information into wp-options table in serialize format.


This function is used to store data in a transient and have three parameters


set_transient($transientKey, $transientValue, $expiration);

$transientKey: transientKey is a unique key for your cached data
$transientValue: transientValue is a value or data you want to save. You can use variable, array or object.
$expiration: Number of seconds to cache the data


This function is used to retrieve a stored data in a transient and have one parameter




This function is used to delete a stored data in a transient and have Transient key parameter.



Now,Let’s go into depth and understand by code:

$transient_key = 'transient_key';
$post_data = get_transient( $transient_key );
if ( $post_data == '' ) {
	$post_data = get_posts( array( 'posts_per_page' => -1 ) );
	set_transient( $transient_key, $post_data, 60 * 24 );
echo $post_data;

Here, above example is get data using get_transient function and if data is not found, store data into transient using set_transient function with expiration time.

You may also like:

To change File/Image Upload Path in WordPress
To Replace Navigation Text to Icon in WordPress
File Upload with WordPress

If you’re interested in reading more about the WordPress Transients API, I would recommend checking out the Codex page.

I hope you’ve get this is the tutorial of the WordPress Transient API useful and some of what you can do with it.
Let me know if there are any other API examples.

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.

Most Popular Posts

Custom Directives in Angular JS

Posted on 3 years ago


Overview of Views in MySQL

Posted on 2 years ago


The Reader’s Poll – June 2015

Posted on 3 years ago


The Reader’s Poll – August 2014

Posted on 4 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 5 years ago