JPD2 Easy Transient Caching Plugin

Josh Pollock - March 08, 2014

My newest plugin, JPD2, makes caching the results of a WP_Query, WP_User_Query and WP_Meta_Query, via the transients API easy. Realize increased performance, by caching the results of your queries, with one simple function.

All saved queries are automatically reset when any post is updated.

You can download it from WordPress.org or clone the GitHub repo.

Note plugin is brand new and is still undergoing testing. Please open an issue on GitHub if you find a bug.

Why Use Transient Caching

The Transients API creates temporary entries into your database. One great use is to store the result of complex queries, which allows WordPress to run one query–to get the transient–instead of many queries, and get the same results.

This article explains very well how that works. This plugin automates the process for you.

How To Use It

Usage is simple via the function jpd2_better_query(). The required arguments are $args, which are arguments to be passed to WP_Query (default,) WP_User_Query or WP_Meta_Query and $name, which is a name for the query. Useful if you wish to manually deleted the transient later with delete_transient().

Optional arguments, $type allows you to use WP_User_Query or WP_Meta_Query instead of WP_Query, and $expire lets you override the default expiration time for the transient.

<?php
/**
* Basic Use
*/
$args = array( 'post_type => 'page', 'posts_per_page' => 3 );
$name = foo;
$foo_query = jpd2_better_query( $args, $name );
/**
* Safest Usage
*/
//WP_Query Example
$args = array( 'post_type => 'page', 'posts_per_page' => 3 );
if ( function_exists( 'jpd2_better_query' ) {
$name = foo;
$foo_query = jpd2_better_query( $args, $name );
}
else {
$foo_query = new WP_Query( $args );
}
//WP_User_Query
$args = array(
'search' => 'Josh',
'search_columns' => array( 'user_login', 'user_email' ),
);
if ( function_exists( 'jpd2_better_query' ) {
$name = foo;
$foo_query = jpd2_better_query( $args, $type='WP_User_Query', $name );
}
else {
$foo_query = new WP_User_Query( $args );
}
//WP_Meta_Query
$args = array(
'relation' => 'OR',
array(
'key' => 'lightsbar_color',
'value' => 'red',
'compare' => '='
)
);
if ( function_exists( 'jpd2_better_query' ) {
$name = foo;
$foo_query = jpd2_better_query( $args, $type='WP_Meta_Query', $name );
}
else {
$foo_query = new WP_User_Query( $args );
}
?>
view raw use_jpd2.php hosted with ❤ by GitHub

By default, transients last up to one day. There are three ways to change that, as shown below:

<?php
/**
* Basic Use
*/
$args = array( 'post_type => 'page', 'posts_per_page' => 3 );
$name = foo;
$foo_query = jpd2_better_query( $args, $name );
/**
* Safest Usage
*/
//WP_Query Example
$args = array( 'post_type => 'page', 'posts_per_page' => 3 );
if ( function_exists( 'jpd2_better_query' ) {
$name = foo;
$foo_query = jpd2_better_query( $args, $name );
}
else {
$foo_query = new WP_Query( $args );
}
//WP_User_Query
$args = array(
'search' => 'Josh',
'search_columns' => array( 'user_login', 'user_email' ),
);
if ( function_exists( 'jpd2_better_query' ) {
$name = foo;
$foo_query = jpd2_better_query( $args, $type='WP_User_Query', $name );
}
else {
$foo_query = new WP_User_Query( $args );
}
//WP_Meta_Query
$args = array(
'relation' => 'OR',
array(
'key' => 'lightsbar_color',
'value' => 'red',
'compare' => '='
)
);
if ( function_exists( 'jpd2_better_query' ) {
$name = foo;
$foo_query = jpd2_better_query( $args, $type='WP_Meta_Query', $name );
}
else {
$foo_query = new WP_User_Query( $args );
}
?>
view raw use_jpd2.php hosted with ❤ by GitHub