Skip to content

Instantly share code, notes, and snippets.

@ChrisLTD
Last active August 29, 2015 14:02
Show Gist options
  • Save ChrisLTD/09f28196fa3e20d5fa9d to your computer and use it in GitHub Desktop.
Save ChrisLTD/09f28196fa3e20d5fa9d to your computer and use it in GitHub Desktop.
<?php
// Function to regenerate the data and save the transients
function special_query(){
$special_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' );
set_transient( 'special_query_results' , $special_query_results, 12 * HOUR_IN_SECONDS );
set_transient( 'special_query_results_no_expire' , $special_query_results, 99 * YEAR_IN_SECONDS );
return $special_query_results;
}
// Get any existing copy of our transient data
if( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ){
// It wasn't there, so let's use the cache that doesn't expire
if( false === ( $special_query_results = get_transient( 'special_query_results_no_expire' ) ) ){
// If for some reason our forever cache is missing, run the query
$special_query_results = special_query();
} else {
// Our forever cache exists, but we need to update the data and save the transient on shutdown
add_action('shutdown', 'special_query');
}
}
// Use the data like you would have normally...
@williameliel
Copy link

It works if you don't need to receive any variables for special_query() to return some data. If special_query were to receive an argument...zing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment