Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to the WordPress Transients API


Published on

An overview of why transients are important as well as some sample code to make them work. Discusses cache invalidation.

Published in: Internet
  • Login to see the comments

  • Be the first to like this

Introduction to the WordPress Transients API

  1. 1. Introduction to the WordPress Transients API Subtitle goes here if needed Presenter Topher DeRosia @topher1kenobe Date 20 July, 2019
  2. 2. What is a Transient, and where would I use it? Time consuming queries Meta queries are slow. Meta queries comparing against multiple fields are even slower. We can cache the results in a transient. Remote data fetched via the HTTP API Using the HTTP API you can fetch data from a remote server. But what if that server goes down? What about latency? We can cache the results in a transient. tran·sient /ˈtranSHənt,ˈtranzēənt/ adjective: transient 1. lasting only for a short time; impermanent. "a transient cold spell" 2. In WordPress, a bit of data cached for an indeterminate amount of time
  3. 3. How do transients work? • Each transient has a name • When you need the data, check to see if it exists • If it does, use it. • If it doesn’t, generate the data and set it for next time
  4. 4. Where are transients stored? • I don’t care • It doesn’t matter • WordPress takes care of it for you
  5. 5. No, really, where are transients stored? • First WordPress looks for a real, system level, caching module like memcached, Redis, APC, Varnish, etc. These are very very fast, and much prefered. • Failing that, transients are store in the WordPress options table. Yes, this is still a database call, but a simple, speedy one. • You don’t have to do anything to set this up, WordPress magically handles it.
  6. 6. How is this magic accomplished? • WordPress provides functions to set, get, and delete transients. Setting one that already exists updates it. • Using these function you simply ask for the data you want. If it’s not there you build it and store it for next time.
  7. 7. Behold the horrible query. $args = [ 'post_status'=> 'publish', 'post_type' => 'coaches', 'orderby' => 'date', 'no_found_rows' => true, 'meta_query' => [ [ ['key'] = 'ecpt_sold'; ['value'] = 'on'; ['compare'] = 'NOT EXISTS'; ], ], 'meta_query' => [ [ ['key'] = 'ecpt_featured'; ['value'] = 'on'; ['compare'] = 'EXISTS'; ], ], ]; $coaches_query = new WP_Query( $args );
  8. 8. Behold the transient code. // create the transient name $transient_name = 'coaches'; // try getting the transient. $coaches = get_transient( $transient_name ); // if the get works properly, I should have an object in $coaches. // If not, run the query. if( ! is_object( $coaches ) ) { // run horrible query here $coaches_query = new WP_Query( $args ); // save the results of the query with a 12 hour timeout $save_query = set_transient( $transient_name, $coaches_query, HOUR_IN_SECONDS * 12 ); $coaches = $coaches_query; } // now $coaches will always hold an object full of Coach
  9. 9. But wait! What about changes? // delete featured coaches transient on coach save function delete_coach_transient( $post_id ) { // First we want to make sure that this is a real save, not simply an auto save if ( ! wp_is_post_revision( $post_id ) ) { // now we declare our custom content type, because we only want to run this on the save of this type $slug = 'coaches'; // this is where we actually make sure we're on the right type. $_POST += array( "{$slug}_edit_nonce" => '' ); if ( $slug != $_POST['post_type'] ) { return; } // assuming we're on the proper type, set the transient name $transient_name = 'coaches'; // now delete the actual transient delete_transient($transient_name); } // end delete_featured_transient } // now we hook that function into save_post and it's all set add_action('save_post','delete_coach_transient');
  10. 10. Important things to remember • You can store just about anything in a transient • Transients are ethereal. Lots of things can delete them, never assume they’re there just because you set them. ALWAYS TEST • Think about when and how to invalidate them. Otherwise you’re stuck with cached content
  11. 11. Resources • Pippin’s Transients Manager • Delete Expired Transients • Query Monitor
  12. 12. Thank You Twitter: @topher1kenobe Make WordPress Slack: topher1kenobe Email: