Skip to content

Instantly share code, notes, and snippets.

@flegfleg
Created September 4, 2020 11:09
Show Gist options
  • Save flegfleg/0cbe366ab157c61163922c159417e21f to your computer and use it in GitHub Desktop.
Save flegfleg/0cbe366ab157c61163922c159417e21f to your computer and use it in GitHub Desktop.
save WordPress query, alter query, reset query
<div class="grid-layout">
<?php
// main query
global $post;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;
$main_query_args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'category' => 1,
'paged' => $paged
);
$main_query = new WP_Query( $main_query_args );
$temp_query = $wp_query; // save the main query to recover it later
$wp_query = NULL;
$wp_query = $main_query; // set wp_query to our main query
$pagination = array(
'base' => '%_%',
'format' => '?page=%#%',
'total' => $main_query->max_num_pages,
'prev_next' => True,
'prev_text' => __( '<< Previous' ),
'next_text' => __( 'Next >>' )
);
if ( $main_query->have_posts() ) :
while ( $main_query->have_posts() ) : $main_query->the_post();
get_template_part( 'template-parts/content', 'grid-posts' );
endwhile;
endif;
?>
</div><!-- grid-layout -->
<?php
$wp_query = NULL;
$wp_query = $temp_query; // reset the wp query
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment