Introduction to the WordPress Transients API

Introduction to the
WordPress Transients API
Subtitle goes here if needed
Presenter
Topher DeRosia
@topher1kenobe
Date
20 July, 2019
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
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
Where are transients stored?
• I don’t care
• It doesn’t matter
• WordPress takes care of it for you
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.
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.
Introduction to the WordPress Transients API
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 );
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
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');
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
Resources
• Pippin’s Transients Manager
https://wordpress.org/plugins/transients-manager/
• Delete Expired Transients
https://wordpress.org/plugins/delete-expired-transients/
• Query Monitor
https://wordpress.org/plugins/query-monitor/
Thank You
Twitter: @topher1kenobe
Make WordPress Slack: topher1kenobe
Email: topher@bigcommerce.com
1 of 13

Recommended

Intro To Moose by
Intro To MooseIntro To Moose
Intro To MoosecPanel
1.3K views22 slides
RSpec 2 Best practices by
RSpec 2 Best practicesRSpec 2 Best practices
RSpec 2 Best practicesAndrea Reginato
1.5M views20 slides
Rapid web development, the right way. by
Rapid web development, the right way.Rapid web development, the right way.
Rapid web development, the right way.nubela
598 views50 slides
Introduction to AngularJS For WordPress Developers by
Introduction to AngularJS For WordPress DevelopersIntroduction to AngularJS For WordPress Developers
Introduction to AngularJS For WordPress DevelopersCaldera Labs
5.6K views23 slides
Inside Bokete: Web Application with Mojolicious and others by
Inside Bokete:  Web Application with Mojolicious and othersInside Bokete:  Web Application with Mojolicious and others
Inside Bokete: Web Application with Mojolicious and othersYusuke Wada
8.2K views37 slides
Bcblackpool jquery tips by
Bcblackpool jquery tipsBcblackpool jquery tips
Bcblackpool jquery tipsJack Franklin
612 views57 slides

More Related Content

What's hot

The Best (and Worst) of Django by
The Best (and Worst) of DjangoThe Best (and Worst) of Django
The Best (and Worst) of DjangoJacob Kaplan-Moss
35.7K views53 slides
In-depth changes to Drupal 8 javascript by
In-depth changes to Drupal 8 javascriptIn-depth changes to Drupal 8 javascript
In-depth changes to Drupal 8 javascriptThéodore Biadala
7.6K views49 slides
Django REST Framework by
Django REST FrameworkDjango REST Framework
Django REST FrameworkLoad Impact
1.9K views22 slides
Django multi-tier by
Django multi-tierDjango multi-tier
Django multi-tiersmirolo
4.1K views10 slides
Google compute presentation puppet conf by
Google compute presentation puppet confGoogle compute presentation puppet conf
Google compute presentation puppet confbodepd
791 views29 slides
Djangocon 2014 angular + django by
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + djangoNina Zakharenko
8.3K views40 slides

What's hot(20)

In-depth changes to Drupal 8 javascript by Théodore Biadala
In-depth changes to Drupal 8 javascriptIn-depth changes to Drupal 8 javascript
In-depth changes to Drupal 8 javascript
Théodore Biadala7.6K views
Django REST Framework by Load Impact
Django REST FrameworkDjango REST Framework
Django REST Framework
Load Impact1.9K views
Django multi-tier by smirolo
Django multi-tierDjango multi-tier
Django multi-tier
smirolo4.1K views
Google compute presentation puppet conf by bodepd
Google compute presentation puppet confGoogle compute presentation puppet conf
Google compute presentation puppet conf
bodepd791 views
Djangocon 2014 angular + django by Nina Zakharenko
Djangocon 2014 angular + djangoDjangocon 2014 angular + django
Djangocon 2014 angular + django
Nina Zakharenko8.3K views
Automated testing with RSpec by Nascenia IT
Automated testing with RSpecAutomated testing with RSpec
Automated testing with RSpec
Nascenia IT5K views
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word... by Caldera Labs
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Single Page Web Apps As WordPress Admin Interfaces Using AngularJS & The Word...
Caldera Labs6.6K views
Sprout core and performance by Yehuda Katz
Sprout core and performanceSprout core and performance
Sprout core and performance
Yehuda Katz1.4K views
jQuery - 10 Time-Savers You (Maybe) Don't Know by girish82
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
girish822.3K views
Rspec API Documentation by SmartLogic
Rspec API DocumentationRspec API Documentation
Rspec API Documentation
SmartLogic2.2K views
Working with WP_Query in WordPress by topher1kenobe
Working with WP_Query in WordPressWorking with WP_Query in WordPress
Working with WP_Query in WordPress
topher1kenobe1.7K views
Gazelle - Plack Handler for performance freaks #yokohamapm by Masahiro Nagano
Gazelle - Plack Handler for performance freaks #yokohamapmGazelle - Plack Handler for performance freaks #yokohamapm
Gazelle - Plack Handler for performance freaks #yokohamapm
Masahiro Nagano13.1K views
Quick MySQL performance check by Tom Diederich
Quick MySQL performance checkQuick MySQL performance check
Quick MySQL performance check
Tom Diederich463 views
Jquery Best Practices by brinsknaps
Jquery Best PracticesJquery Best Practices
Jquery Best Practices
brinsknaps3.4K views
RSpec User Stories by rahoulb
RSpec User StoriesRSpec User Stories
RSpec User Stories
rahoulb4.2K views
N:1 Replication meets MHA by do_aki
N:1 Replication meets MHAN:1 Replication meets MHA
N:1 Replication meets MHA
do_aki11.9K views

Similar to Introduction to the WordPress Transients API

Best Practices in Plugin Development (WordCamp Seattle) by
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)andrewnacin
27.1K views47 slides
Transients are good for you - WordCamp London 2016 by
Transients are good for you - WordCamp London 2016Transients are good for you - WordCamp London 2016
Transients are good for you - WordCamp London 2016Boiteaweb
2.3K views49 slides
Intro to advanced caching in WordPress by
Intro to advanced caching in WordPressIntro to advanced caching in WordPress
Intro to advanced caching in WordPressMaor Chasen
1.5K views43 slides
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul... by
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...andrewnacin
4.3K views27 slides
Options, and Transients, and Theme Mods — Oh my! by
Options, and Transients, and Theme Mods — Oh my!Options, and Transients, and Theme Mods — Oh my!
Options, and Transients, and Theme Mods — Oh my!Konstantin Obenland
2.7K views40 slides
Php security3895 by
Php security3895Php security3895
Php security3895PrinceGuru MS
1.6K views38 slides

Similar to Introduction to the WordPress Transients API(20)

Best Practices in Plugin Development (WordCamp Seattle) by andrewnacin
Best Practices in Plugin Development (WordCamp Seattle)Best Practices in Plugin Development (WordCamp Seattle)
Best Practices in Plugin Development (WordCamp Seattle)
andrewnacin27.1K views
Transients are good for you - WordCamp London 2016 by Boiteaweb
Transients are good for you - WordCamp London 2016Transients are good for you - WordCamp London 2016
Transients are good for you - WordCamp London 2016
Boiteaweb2.3K views
Intro to advanced caching in WordPress by Maor Chasen
Intro to advanced caching in WordPressIntro to advanced caching in WordPress
Intro to advanced caching in WordPress
Maor Chasen1.5K views
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul... by andrewnacin
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
WordCamp San Francisco 2011: Transients, Caching, and the Complexities of Mul...
andrewnacin4.3K views
Options, and Transients, and Theme Mods — Oh my! by Konstantin Obenland
Options, and Transients, and Theme Mods — Oh my!Options, and Transients, and Theme Mods — Oh my!
Options, and Transients, and Theme Mods — Oh my!
Konstantin Obenland2.7K views
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014 by Cliff Seal
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Temporary Cache Assistance (Transients API): WordCamp Birmingham 2014
Cliff Seal1.6K views
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014 by Cliff Seal
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Temporary Cache Assistance (Transients API): WordCamp Phoenix 2014
Cliff Seal5K views
Speed Things Up with Transients by Cliff Seal
Speed Things Up with TransientsSpeed Things Up with Transients
Speed Things Up with Transients
Cliff Seal1.5K views
Php Security3895 by Aung Khant
Php Security3895Php Security3895
Php Security3895
Aung Khant495 views
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013) by arcware
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
10 Things Every Plugin Developer Should Know (WordCamp Atlanta 2013)
arcware4.2K views
Php Code Audits (PHP UK 2010) by Damien Seguy
Php Code Audits (PHP UK 2010)Php Code Audits (PHP UK 2010)
Php Code Audits (PHP UK 2010)
Damien Seguy3K views
jQuery Anti-Patterns for Performance & Compression by Paul Irish
jQuery Anti-Patterns for Performance & CompressionjQuery Anti-Patterns for Performance & Compression
jQuery Anti-Patterns for Performance & Compression
Paul Irish25.2K views
You don’t know query - WordCamp UK Edinburgh 2012 by l3rady
You don’t know query - WordCamp UK Edinburgh 2012You don’t know query - WordCamp UK Edinburgh 2012
You don’t know query - WordCamp UK Edinburgh 2012
l3rady978 views
WordPress as an application framework by Dustin Filippini
WordPress as an application frameworkWordPress as an application framework
WordPress as an application framework
Dustin Filippini416 views
Sql storeprocedure by ftz 420
Sql storeprocedureSql storeprocedure
Sql storeprocedure
ftz 4201.5K views
You Don't Know Query - WordCamp Portland 2011 by andrewnacin
You Don't Know Query - WordCamp Portland 2011You Don't Know Query - WordCamp Portland 2011
You Don't Know Query - WordCamp Portland 2011
andrewnacin11.1K views
jQuery Anti-Patterns for Performance by András Kovács
jQuery Anti-Patterns for PerformancejQuery Anti-Patterns for Performance
jQuery Anti-Patterns for Performance
András Kovács1K views

More from topher1kenobe

How To Increase Ecommerce Conversions by
How To Increase Ecommerce ConversionsHow To Increase Ecommerce Conversions
How To Increase Ecommerce Conversionstopher1kenobe
519 views22 slides
Build Ecommerce Sites With Confidence (Demystifying Ecommerce), WordCamp Los ... by
Build Ecommerce Sites With Confidence (Demystifying Ecommerce), WordCamp Los ...Build Ecommerce Sites With Confidence (Demystifying Ecommerce), WordCamp Los ...
Build Ecommerce Sites With Confidence (Demystifying Ecommerce), WordCamp Los ...topher1kenobe
1.3K views26 slides
Build Ecommerce Sites With Confidence (Demystifying Ecommerce) by
Build Ecommerce Sites With Confidence (Demystifying Ecommerce)Build Ecommerce Sites With Confidence (Demystifying Ecommerce)
Build Ecommerce Sites With Confidence (Demystifying Ecommerce)topher1kenobe
184 views26 slides
6 Ecommerce Trends Altering the Ecommerce Landscape, and changing which strat... by
6 Ecommerce Trends Altering the Ecommerce Landscape, and changing which strat...6 Ecommerce Trends Altering the Ecommerce Landscape, and changing which strat...
6 Ecommerce Trends Altering the Ecommerce Landscape, and changing which strat...topher1kenobe
255 views26 slides
Talking to Other Sites with the WP HTTP API by
Talking to Other Sites with the WP HTTP APITalking to Other Sites with the WP HTTP API
Talking to Other Sites with the WP HTTP APItopher1kenobe
320 views5 slides
What’s a REST API and why should I care? by
What’s a REST API and why should I care?What’s a REST API and why should I care?
What’s a REST API and why should I care?topher1kenobe
684 views11 slides

More from topher1kenobe(13)

How To Increase Ecommerce Conversions by topher1kenobe
How To Increase Ecommerce ConversionsHow To Increase Ecommerce Conversions
How To Increase Ecommerce Conversions
topher1kenobe519 views
Build Ecommerce Sites With Confidence (Demystifying Ecommerce), WordCamp Los ... by topher1kenobe
Build Ecommerce Sites With Confidence (Demystifying Ecommerce), WordCamp Los ...Build Ecommerce Sites With Confidence (Demystifying Ecommerce), WordCamp Los ...
Build Ecommerce Sites With Confidence (Demystifying Ecommerce), WordCamp Los ...
topher1kenobe1.3K views
Build Ecommerce Sites With Confidence (Demystifying Ecommerce) by topher1kenobe
Build Ecommerce Sites With Confidence (Demystifying Ecommerce)Build Ecommerce Sites With Confidence (Demystifying Ecommerce)
Build Ecommerce Sites With Confidence (Demystifying Ecommerce)
topher1kenobe184 views
6 Ecommerce Trends Altering the Ecommerce Landscape, and changing which strat... by topher1kenobe
6 Ecommerce Trends Altering the Ecommerce Landscape, and changing which strat...6 Ecommerce Trends Altering the Ecommerce Landscape, and changing which strat...
6 Ecommerce Trends Altering the Ecommerce Landscape, and changing which strat...
topher1kenobe255 views
Talking to Other Sites with the WP HTTP API by topher1kenobe
Talking to Other Sites with the WP HTTP APITalking to Other Sites with the WP HTTP API
Talking to Other Sites with the WP HTTP API
topher1kenobe320 views
What’s a REST API and why should I care? by topher1kenobe
What’s a REST API and why should I care?What’s a REST API and why should I care?
What’s a REST API and why should I care?
topher1kenobe684 views
Custom Database Queries in WordPress by topher1kenobe
Custom Database Queries in WordPressCustom Database Queries in WordPress
Custom Database Queries in WordPress
topher1kenobe4.9K views
Introduction to WordPress Child Theming, WordCamp Kansas City, 2015 by topher1kenobe
Introduction to WordPress Child Theming, WordCamp Kansas City, 2015Introduction to WordPress Child Theming, WordCamp Kansas City, 2015
Introduction to WordPress Child Theming, WordCamp Kansas City, 2015
topher1kenobe1.8K views
Intro to Plugin Development, Miami WordCamp, 2015 by topher1kenobe
Intro to Plugin Development, Miami WordCamp, 2015Intro to Plugin Development, Miami WordCamp, 2015
Intro to Plugin Development, Miami WordCamp, 2015
topher1kenobe1.5K views
Introduction to WordPress Plugin Development, WordCamp North Canton, 2015 by topher1kenobe
Introduction to WordPress Plugin Development, WordCamp North Canton, 2015Introduction to WordPress Plugin Development, WordCamp North Canton, 2015
Introduction to WordPress Plugin Development, WordCamp North Canton, 2015
topher1kenobe906 views
WordCamp Ann Arbor 2014: Site Caching, From Nothing to Everything by topher1kenobe
WordCamp Ann Arbor 2014: Site Caching, From Nothing to EverythingWordCamp Ann Arbor 2014: Site Caching, From Nothing to Everything
WordCamp Ann Arbor 2014: Site Caching, From Nothing to Everything
topher1kenobe738 views
Command Line Awesome, WordCamp Grand Rapids 2014 by topher1kenobe
Command Line Awesome, WordCamp Grand Rapids 2014Command Line Awesome, WordCamp Grand Rapids 2014
Command Line Awesome, WordCamp Grand Rapids 2014
topher1kenobe2K views

Recently uploaded

cis5-Project-11a-Harry Lai by
cis5-Project-11a-Harry Laicis5-Project-11a-Harry Lai
cis5-Project-11a-Harry Laiharrylai126
9 views11 slides
The Dark Web : Hidden Services by
The Dark Web : Hidden ServicesThe Dark Web : Hidden Services
The Dark Web : Hidden ServicesAnshu Singh
22 views24 slides
40th TWNIC Open Policy Meeting: A quick look at QUIC by
40th TWNIC Open Policy Meeting: A quick look at QUIC40th TWNIC Open Policy Meeting: A quick look at QUIC
40th TWNIC Open Policy Meeting: A quick look at QUICAPNIC
109 views20 slides
ARNAB12.pdf by
ARNAB12.pdfARNAB12.pdf
ARNAB12.pdfArnabChakraborty499766
5 views83 slides
Liberando a produccion con confidencia.pdf by
Liberando a produccion con confidencia.pdfLiberando a produccion con confidencia.pdf
Liberando a produccion con confidencia.pdfAndres Almiray
6 views49 slides
Affiliate Marketing by
Affiliate MarketingAffiliate Marketing
Affiliate MarketingNavin Dhanuka
21 views30 slides

Recently uploaded(15)

cis5-Project-11a-Harry Lai by harrylai126
cis5-Project-11a-Harry Laicis5-Project-11a-Harry Lai
cis5-Project-11a-Harry Lai
harrylai1269 views
The Dark Web : Hidden Services by Anshu Singh
The Dark Web : Hidden ServicesThe Dark Web : Hidden Services
The Dark Web : Hidden Services
Anshu Singh22 views
40th TWNIC Open Policy Meeting: A quick look at QUIC by APNIC
40th TWNIC Open Policy Meeting: A quick look at QUIC40th TWNIC Open Policy Meeting: A quick look at QUIC
40th TWNIC Open Policy Meeting: A quick look at QUIC
APNIC109 views
Liberando a produccion con confidencia.pdf by Andres Almiray
Liberando a produccion con confidencia.pdfLiberando a produccion con confidencia.pdf
Liberando a produccion con confidencia.pdf
Andres Almiray6 views
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptx by LeasedLinesQuote
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptxCracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptx
Cracking the Code Decoding Leased Line Quotes for Connectivity Excellence.pptx
40th TWNIC OPM: On LEOs (Low Earth Orbits) and Starlink Download by APNIC
40th TWNIC OPM: On LEOs (Low Earth Orbits) and Starlink Download40th TWNIC OPM: On LEOs (Low Earth Orbits) and Starlink Download
40th TWNIC OPM: On LEOs (Low Earth Orbits) and Starlink Download
APNIC112 views
ATPMOUSE_융합2조.pptx by kts120898
ATPMOUSE_융합2조.pptxATPMOUSE_융합2조.pptx
ATPMOUSE_융합2조.pptx
kts12089835 views
Penetration Testing for Cybersecurity Professionals by 211 Check
Penetration Testing for Cybersecurity ProfessionalsPenetration Testing for Cybersecurity Professionals
Penetration Testing for Cybersecurity Professionals
211 Check49 views
40th TWNIC Open Policy Meeting: APNIC PDP update by APNIC
40th TWNIC Open Policy Meeting: APNIC PDP update40th TWNIC Open Policy Meeting: APNIC PDP update
40th TWNIC Open Policy Meeting: APNIC PDP update
APNIC106 views
WITS Deck by W.I.T.S.
WITS DeckWITS Deck
WITS Deck
W.I.T.S.36 views

Introduction to the WordPress Transients API

  • 1. Introduction to the WordPress Transients API Subtitle goes here if needed Presenter Topher DeRosia @topher1kenobe Date 20 July, 2019
  • 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. 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. Where are transients stored? • I don’t care • It doesn’t matter • WordPress takes care of it for you
  • 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. 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.
  • 8. 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 );
  • 9. 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
  • 10. 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');
  • 11. 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
  • 12. Resources • Pippin’s Transients Manager https://wordpress.org/plugins/transients-manager/ • Delete Expired Transients https://wordpress.org/plugins/delete-expired-transients/ • Query Monitor https://wordpress.org/plugins/query-monitor/
  • 13. Thank You Twitter: @topher1kenobe Make WordPress Slack: topher1kenobe Email: topher@bigcommerce.com