Skip to content

Instantly share code, notes, and snippets.

@jonnymaceachern
Created July 20, 2017 16:49
Show Gist options
  • Save jonnymaceachern/c0e4808afae31375c8901780074d508e to your computer and use it in GitHub Desktop.
Save jonnymaceachern/c0e4808afae31375c8901780074d508e to your computer and use it in GitHub Desktop.
<?php
// Define custom query parameters
$custom_query_args = array( /* Parameters go here */ );
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
endwhile;
endif;
// Reset postdata
wp_reset_postdata();
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment