The problem statement

For some reason when the user does a search the results page (search.php) only show the first page results. The pagination (next and previous links) also believes its on page 1.

The URL structure is https://www.domianname.com/page/1?s=query.

You can change out the number for 2, 3, 4 etc and it won't 404 but it will still show page 1 results.

Is this an issue with the WP_Query or could it be the custom URL structure (/page/1)?

Snippet of the search.php file

 $paged = $page
 $loop = new WP_Query(
   'posts_per_page' => '20',
   'paged' => $paged
 if ( $loop->have_posts() ) { ?>
  <div class="results">
   <?php while ( $loop->have_posts() ) { 
    $post_id = $post->ID;
    <div class="article">
     <h3><a href="<?php the_permalink();?>">the_title(); ?></a></h3>
     <!-- A bunch of if conditions depending on post type-->
    </div><!--close article-->
   <?php } ?>
  </div><!--close results-->
   if( get_previous_posts_link() || get_next_posts_link() ) {
  <div class="navigation"> 
   if( get_previous_posts_link() ) {
    echo '<span class="newer">'.get_previous_posts_link( "Previous Page", $loop->max_num_pages).'</span>';
    if( get_next_posts_link() ) {
     echo '<span class="older">'.get_next_posts_link( "Next Page", $loop->max_num_pages).'</span>';
<?php } ?>
<?php } else { ?>
 <p>There are no results for that search query. Please search again.</p>
<?php } ?>

1 Answer 1


You are setting $paged = $page which I suspect may be at least one of your issues.

Try changing that line to:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

or if the page is static:

$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;

It is also worth noting that by creating a custom search query you are ignoring/altering the main query after it has run, which can cause issue with the pagination links. It is usually better to amend the query in functions.php via "pre_get_posts" before the query is executed rather than in the search.php page template after-the-fact.

You may need to include the following after the loop.

// Reset the $post data to the current post in main query.

https://developer.wordpress.org/reference/hooks/pre_get_posts/ https://developer.wordpress.org/reference/functions/wp_reset_postdata/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.