Skip to content

Instantly share code, notes, and snippets.

@WebEndevSnippets
Created January 21, 2013 18:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WebEndevSnippets/4587952 to your computer and use it in GitHub Desktop.
Save WebEndevSnippets/4587952 to your computer and use it in GitHub Desktop.
WordPress: Custom Loop with WP_Query and Transient
function shireman_books_header_loop( $args ) {
global $post;
$defaults = array(
'orderby' => 'rand',
'post_type' => 'we_published-book',
'posts_per_page' => 9,
'post_status' => 'publish',
'no_found_rows' => true, // counts posts, remove if pagination required
'update_post_term_cache' => false, // grabs terms, remove if terms required (category, tag...)
'update_post_meta_cache' => false, // grabs post meta, remove if post meta required
);
$args = wp_parse_args( $args, $defaults );
if ( false === ( $bookthumbs = get_transient( 'we_books_header_' . $post->ID ) ) ) {
$bookthumbs = new WP_Query( $args );
// Set transient for 1 day
set_transient( 'we_books_header_' . $post->ID, $bookthumbs, 60*60*24 );
}
if ( $bookthumbs->have_posts() ) {
echo '<ul>';
while ( $bookthumbs->have_posts() ) : $bookthumbs->the_post();
if( has_post_thumbnail() ) {
printf( '<li><a href="%s" title="%s"><span class="roll" ></span>%s</a></li>', get_permalink(), get_the_title(), genesis_get_image( array( 'format' => 'html', 'size' => 'book-small', ) ) );
}
endwhile;
echo '</ul>';
}
wp_reset_query();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment