Skip to content

Instantly share code, notes, and snippets.

@hsleonis
Created March 30, 2016 08:35
Show Gist options
  • Select an option

  • Save hsleonis/8c24d8828a1c28494730ef7ca9917438 to your computer and use it in GitHub Desktop.

Select an option

Save hsleonis/8c24d8828a1c28494730ef7ca9917438 to your computer and use it in GitHub Desktop.
<?php
/**
* The Transients API is very similar to the Options API but with the added feature of an expiration time,
* which simplifies the process of using the wp_options database table to temporarily store cached information.
* Reference: https://codex.wordpress.org/Transients_API
*/
// Get any existing copy of our transient data
if ( false === ( $special_query_results = get_transient( 'special_query_results' ) ) ) {
// It wasn't there, so regenerate the data and save the transient
$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 );
}
// Use the data like you would have normally...
// Create a simple function to delete our transient
function edit_term_delete_transient() {
delete_transient( 'special_query_results' );
}
// Add the function to the edit_term hook so it runs when categories/tags are edited
add_action( 'edit_term', 'edit_term_delete_transient' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment