WordPress Transients

20
Transients: Temporary Cache Assistance

Transcript of WordPress Transients

Transients:Temporary Cache Assistance

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

set_transient( $transient, $value, $expiration);

$transient• (string) a unique identifier for your cached data

• 45 characters or less

$value• (array|object) Data to save, either a regular variable or

an array/object

• Handles serialization of complex data for you

$expiration• (integer) number of seconds to keep the data before

refreshing

• Example: 60*60*24 or 86400 (24 hours)

MINUTE_IN_SECONDS = 60HOUR_IN_SECONDS = 60 * MINUTE_IN_SECONDSDAY_IN_SECONDS = 24 * HOUR_IN_SECONDSWEEK_IN_SECONDS = 7 * DAY_IN_SECONDSYEAR_IN_SECONDS = 365 * DAY_IN_SECONDS

get_transient($transient);

• Returns false if the transient doesn’t exist or has expired

• An integar value of zero/empty could be the stored data, so always check for false

• Should not be used to hold plain boolean values; use array, string, or integer instead

delete_transient($transient);

Once a transient expires, it remains in the database until it is refreshed or deleted.

Basic Example:Store a query

function ut_get_ticker_items() { $args = array( 'post_type' => array( 'news-items', 'event' ), 'posts_per_page' => 99, 'taxonomy' => 'news_categories', 'term' => 'ticker', ); $posts = new WP_Query( $args ); return $posts;}

function ut_get_ticker_items() { if ( false === ( $posts = get_transient( 'ticker_items' ) ) ) { $args = array( 'post_type' => array( 'news-items', 'event' ), 'posts_per_page' => 99, 'taxonomy' => 'news_categories', 'term' => 'ticker', ); $posts = new WP_Query( $args ); set_transient( 'ticker_items', $posts, WEEK_IN_SECONDS ); } return $posts;}

function ut_refresh_ticker_items( $post_id, $post_obj ) { $post_type = $post_obj->post_type; if ( in_array( $post_type, array( 'news-items', 'event' ) ) ) { delete_transient( 'ticker_items' ); }}

add_action( 'save_post', 'ut_refresh_ticker_items', 10, 2 );add_action( 'deleted_post', 'ut_refresh_ticker_items', 10, 2 );

Advanced Example:Store user’s search results

function ws_set_search_string() { global $wp_query; $wp_session = WP_Session::get_instance(); if ( is_admin() === false && is_search() === true && isset( $wp_query->query_vars_hash ) === true ) { $search_string = $wp_query->query['s']; $search_hash = $wp_query->query_vars_hash; $wp_session['search_string'] = $search_string; $wp_session['search_hash'] = $search_hash; $wp_session['search_array'] = ws_search_array( $search_hash ); } if ( is_admin() === false && is_search() === false && is_attachment() === false && is_main_query() === true ) { $wp_session['search_string'] = null; $wp_session['search_hash'] = null; $wp_session['search_array'] = null; }}add_action( 'pre_get_posts', 'ws_set_search_string' );

function ws_search_array( $search_hash ) { if ( false === ( $search_array = get_transient( 'ws_search_' . $search_hash ) ) ) { global $wpdb; $posts = $wpdb->get_results( $GLOBALS['wp_query']->request );

if ( empty( $posts ) === false ) { $search_array = array(); foreach ( $posts as $post ) { $search_array[] = $post->ID; } } // save the transient for 10 minutes set_transient( 'ws_search_' . $search_hash, $search_array, HOURS_IN_SECONDS * 6 ); } return $search_array;}

function ws_get_next_search_result( $next = true ) { $wp_session = WP_Session::get_instance(); if ( isset( $wp_session['search_array'] ) === false ) { return false; } $next_location = 0; $search_array = $wp_session['search_array']->toArray(); $location = array_search( get_the_ID(), $search_array ); if ( $next === true ) { $next_location = $location + 1; if ( isset( $search_array[$next_location] ) === false ) { $next_location = 0; } } else { $next_location = $location - 1; if ( isset( $search_array[$next_location] ) === false ) { end( $search_array ); $next_location = key( $search_array ); } } return $search_array[$next_location];}

function ws_post_nav() { $prev = $next = null; if ( ws_get_next_search_result() ) { $prev = get_post( ws_get_next_search_result( false ) ); $next = get_post( ws_get_next_search_result() ); } else { $prev = get_previous_post( true ); $next = get_next_post( true ); }

// output previous and next post links with get_permalink( $next->ID )

}

The End