Skip to content

Instantly share code, notes, and snippets.

@mikaelz
Created May 29, 2013 04:40
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 mikaelz/5667994 to your computer and use it in GitHub Desktop.
Save mikaelz/5667994 to your computer and use it in GitHub Desktop.
WordPress secondary loop
// video source http://wordpress.tv/2013/03/15/andrew-nacin-wp_query-wordpress-in-depth/
// presentation http://www.slideshare.net/andrewnacin/you-dont-know-query-wordcamp-netherlands-2012
$q = new WP_Query();
while ( $q->have_posts() ):
$q->the_post();
endwhile;
wp_reset_postdata();
// or in the template functions.php
function mz_my_template( $q ) {
// always check to avoid modifying main query
if ( !$q->is_main_query() )
return;
if ( !is_page_template('my-template.php') )
return;
$q->set( 'author', -5 );
$q->set( 'posts_per_page', 25 );
}
add_action( 'pre_get_posts', 'mz_my_template' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment