-
-
Save DavidWells/6361166 to your computer and use it in GitHub Desktop.
Wordpress loop transient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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