Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Jany-M
Last active February 2, 2016 18:05
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 Jany-M/b3e92c5094ea9ff65e4a to your computer and use it in GitHub Desktop.
Save Jany-M/b3e92c5094ea9ff65e4a to your computer and use it in GitHub Desktop.
[WordPress] Repeat a Query/Loop with automatic offset, query caching through transients and cutom post type, no children, order by title
<?php
$i = 0;
$num = 30;
$offset = 30;
$cpt = 'rate-plan';
$qty = '1,2,3,4,5,6,7,8,9,10,11,12';
$times = explode(',', $qty);
foreach($times as $item) {
$i++;
$new_offset = ($i*$offset)-30;
?>
<div class="col-md-1">
<div class="row">
<?php
// Check if values are cached, if not cache them
$colnum_transient = 'rp_all'.$i.'_24h';
$colnum_query = 'rp_all'.$i;
//delete_transient('rp_all'.$i.'_24h');
if(get_transient($colnum_transient) === false) {
$colnum_query = new WP_Query(array(
'posts_per_page' => $num,
'offset' => $new_offset,
'post_type' => $cpt,
'post_parent' => 0,
'orderby' => 'title',
'order' => 'ASC'
));
//Cache Results
set_transient($colnum_transient, $colnum_query, 24 * HOUR_IN_SECONDS );
}
$colnum_query = get_transient($colnum_transient);
if ($colnum_query->have_posts()) : while ( $colnum_query->have_posts() ) : $colnum_query->the_post();
?>
<div class="col-md-12">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php
endwhile; endif; wp_reset_query(); wp_reset_postdata();
?>
</div>
</div>
<?php }
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment