Skip to content

Instantly share code, notes, and snippets.

@flashingcursor
Created August 4, 2012 19:53
Show Gist options
  • Save flashingcursor/3259586 to your computer and use it in GitHub Desktop.
Save flashingcursor/3259586 to your computer and use it in GitHub Desktop.
Use Transients API for Caching in Wordpress
<?php
function hwp_cached_query() {
if ( false === ( $hwp_query_results = get_transient( 'hwp_query_results' ) ) ) {
$hwp_query_results = new WP_Query( 'cat=5&order=random&tag=tech&post_meta_key=thumbnail' );
set_transient( 'hwp_query_results', $hwp_query_results, 60*60*24 );
}
while ( $hwp_query_results->have_posts() ) : $hwp_query_results->the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment