Skip to content

Instantly share code, notes, and snippets.

@shadyvb
Created April 19, 2016 12:14
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 shadyvb/5b88abd77fdc475d8c5680cdc6cbfa80 to your computer and use it in GitHub Desktop.
Save shadyvb/5b88abd77fdc475d8c5680cdc6cbfa80 to your computer and use it in GitHub Desktop.
Using a generator function for a simple post query loop
<?php
function get_latest_of_type( $post_type, $number ) {
$args = array(
'post_type' => $post_type,
'posts_per_page' => min( 10, $number ),
'no_found_rows' => true,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
yield $query->post;
}
wp_reset_query();
}
?>
<?php foreach ( get_latest_of_type( 'job', 5 ) as $post ): ?>
<li>
<h5><?php the_title() ?></h5>
<div><?php the_excerpt(); ?></div>
</li>
<?php endforeach; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment