Skip to content

Instantly share code, notes, and snippets.

@aprakasa
Created December 12, 2012 02:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save aprakasa/4264372 to your computer and use it in GitHub Desktop.
Save aprakasa/4264372 to your computer and use it in GitHub Desktop.
Using transients on a single loop inside WordPress
<?php
/**
* Using transients on a single loop inside WordPress
*
* @link http://speckyboy.com/2011/12/14/website-speed-part-3-caching-wordpress/
* /
$loop = get_transient( 'loop' );
if ( false === $loop ) {
// Show the last 100 tweets from the custom post type tweets.
$query = array('post_per_page' => 100,
'post_type' => 'tweets',
'post_status' => 'publish' ) ;
$loop = new WP_Query($query);
// transient set to last for 1 hour
set_transient('loop', $loop, 60*60);
}
// do normal loop stuff
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
// show content or whatever you like
the_content();
endwhile;endif;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment