Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save obiPlabon/4886408c8cad59aec928b6d5ff73a3c5 to your computer and use it in GitHub Desktop.
Save obiPlabon/4886408c8cad59aec928b6d5ff73a3c5 to your computer and use it in GitHub Desktop.
Solution for WP pagination issue on static home page which occurred due to global $paged variable
<?php
global $paged;
// Store the global {$paged} value in a temporary {$_temp_paged} variable
$_temp_paged = $paged;
// Figure out the current page number
$_paged = get_query_var( 'paged' );
$_paged = ( ! empty( $paged ) ? $paged : get_query_var( 'page', 1 ) );
// Now store the {$_paged} value in the global {$paged} variable
$paged = $_paged;
$args = array(
'post_type' => 'post', //any post type
'posts_per_page' => 3,
'post_status' => 'publish',
'order' => 'DESC',
'paged' => $paged
);
$query = new \WP_Query( $args );
while ( $query->have_posts() ) :
$query->the_post();
the_title( '<h2>', '</h2>' );
endwhile;
wp_reset_postdata();
?>
<a href="<?php next_posts( $query->max_num_pages ); ?>">Load more</a>
<?php
// Finally restore the global {$paged} variable using {$_temp_paged}
// make sure to restore this after the load more button
$paged = $_temp_paged;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment