Skip to content

Instantly share code, notes, and snippets.

@astockwell
Last active December 14, 2015 11:38
Show Gist options
  • Save astockwell/5080120 to your computer and use it in GitHub Desktop.
Save astockwell/5080120 to your computer and use it in GitHub Desktop.
Pristine Wordpress loop examples
<?php // Referencing http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts ?>
<?php
// EXAMPLE new WP_Query object
?>
<?php
// Query posts by new criteria.
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
// 'tax_query' => array(
// array(
// 'taxonomy' => 'products-types',
// 'field' => 'slug',
// 'terms' => array($term->slug)
// )
// ),
'order' => 'ASC',
'orderby' => 'menu_order'
);
$my_query = new WP_Query( $args );
// Does $my_query have posts?
if($my_query->have_posts()):
while($my_query->have_posts()): $my_query->the_post(); ?>
<li>
<h3><?php the_title(); ?></h3>
<?php if($thumbnail): ?>
<img src="<?php echo $thumbnail[url]; ?>" />
<?php endif; ?>
<p><?php the_content(); ?></p>
</li>
<?php endwhile; // End while $my_query->have_posts
endif; // End if $my_query->have_posts
wp_reset_postdata(); ?>
<?php
// EXAMPLE get_posts()
?>
... To come ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment