Created
May 29, 2013 04:40
-
-
Save mikaelz/5667994 to your computer and use it in GitHub Desktop.
WordPress secondary loop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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