Skip to content

Instantly share code, notes, and snippets.

@miziomon
Created May 3, 2012 12:17
Show Gist options
  • Save miziomon/2585278 to your computer and use it in GitHub Desktop.
Save miziomon/2585278 to your computer and use it in GitHub Desktop.
Little collection of WordPress utility
/*
* Apply transient cache and return posts (like get_posts)
*
* @param array $args query params WP_Query
* @param string $transient_name
* @param string $transient_expiration Optional defaul 7200
*/
function cache_posts( $args , $transient_name , $transient_expiration = 7200 ){
if ( false === ( $posts = get_transient( $transient_name ) ) ) {
wp_reset_query();
$the_query = new WP_Query();
$the_query->query( $args );
$posts = $the_query->posts;
set_transient( $transient_name , $posts , $transient_expiration );
}
return $posts;
}
/*
* Loop query result and get foreach post call get_template_part
*
* @param object $posts accept WP_Query->posts or get_posts()
* @param string $template_slug name of template
*/
function loop_template( $posts, $template_slug , $template_name = null ){
global $post;
$post_temp = $post;
foreach($posts as $post) :
setup_postdata($post);
get_template_part( $template_slug , $template_name );
endforeach;
$post = $post_temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment