Skip to content

Instantly share code, notes, and snippets.

@mzo84
Created August 16, 2016 06:33
Show Gist options
  • Save mzo84/5cc9754501a1db253d49c314adf6be5c to your computer and use it in GitHub Desktop.
Save mzo84/5cc9754501a1db253d49c314adf6be5c to your computer and use it in GitHub Desktop.
Customizing the Loop with WP_Query
<?php // The WordPress Loop - customized with WP_Query
$custom_query = new WP_Query('cat=-9'); // exclude Asides category
while($custom_query->have_posts()) : $custom_query->the_post();
...
endwhile;
wp_reset_postdata(); // reset the query
?>
// more examples
<?php
$custom_query = new WP_Query('cat=-7,-8,-9'); // exclude any categories
$custom_query = new WP_Query('posts_per_page=3'); // limit number of posts
$custom_query = new WP_Query('order=ASC'); // reverse the post order
$custom_query = new WP_Query('cat=-7,-8,-9&posts_per_page=3&order=ASC');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment