Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ihorvorotnov/9023402 to your computer and use it in GitHub Desktop.
Save ihorvorotnov/9023402 to your computer and use it in GitHub Desktop.
<?php
/**
* Setup wp_query arguments for the loop. Cache the results for 4 hours.
*
* @link http://codex.wordpress.org/Transients_API
*/
// Check for transient
if ( ! ( $my_query = get_transient( 'foo_featured_posts' ) ) ) {
// If transient isn't set, execute WP_Query
$my_query = new WP_Query(
array(
'category' => 'featured',
'posts_per_page' => 5
)
);
// Create transient and expire after 4 hours
set_transient( 'foo_featured_posts', $my_query, 4 * HOUR_IN_SECONDS );
}
/**
* Run the loop.
*
* @link http://codex.wordpress.org/The_Loop
*/
if ( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<!-- Do stuff -->
<?php endwhile; else : ?>
<p>Sorry, no posts found</p>
<?php endif; wp_reset_postdata(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment