Pagination repeating posts on search results

Question

i have a problem with my wordpress search result pagination. Basically it’s working as intended until i hit page 2, it then repeats the results of page one. Here is my search.php, please excuse the messy formatting.. i’m a beginner and have been hacking at it for days. Cheers in advance!

<?php get_header(); ?>


<div class="inner">
<div class="Search-results"
<?php  _e("<h3>Search Results for: ".get_query_var('s')."</h3>"); ?>
</div>

<!-- Boxes -->
<div class="thumbnails">
        
<?php
$s=get_search_query();
$args = array('s' =>$s);
    // The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
       
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div class="box">
 
<div class="image fit"><?php the_post_thumbnail(array( 250,225 ), array( 'class' => 'alignleft' )); ?> </div>
 
<div class="post-details"><?php the_time('d/m/Y'); ?></div>
<div class="post-details2"><?php if(function_exists('the_views')) { the_views(); } ?></div>
 
<div class="inner">
<a href="<?php the_permalink()?>"><h3><?php the_title();?></h3></a>
<p><?php the_content(''); ?></p>
</div>

             
<a href="<?php the_permalink() ?>" class="button fit">Check it out!</a>
</div>

<?php
        }
    }else{
?>
        
<p>Sorry, but nothing matched your search criteria. Please try again with some different keywords.</p>
  
<?php } ?>

<?php wpbeginner_numeric_posts_nav(); ?> 

    
                       </div>
                    </div>
                </div>

 

<?php get_footer(); ?>

Here is my search form

<!-- SEARCH -->
<div class="select-wrapper-top">            
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search">
<input type="text" class="field" name="s" value="<?php echo esc_attr( get_search_query() ); ?>" id="s" placeholder="<?php esc_attr_e( 'Search &hellip;', 'shape' ); ?>" />
<button type="submit" class="btn">
<i class="fa fa-search-plus fa-lg"></i> 
</button>
</form>
</div>
<!-- END SEARCH --> 

…and here are my page numbers if applicable in functions.php

   /** START PAGE NUMBERS */

function wpbeginner_numeric_posts_nav() {
 
    if( is_singular() )
        return;
 
    global $wp_query;
 
    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;
 
    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );
 
    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;
 
    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }
 
    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }
 
    echo '<div class="navigation"><ul>' . "n";
 
    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<li>%s</li>' . "n", get_previous_posts_link('Previous') );
 
    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';
 
        printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
 
        if ( ! in_array( 2, $links ) )
            echo '<li>-</li>';
    }
 
    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }
 
    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<li>-</li>' . "n";
 
        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }
 
    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li>%s</li>' . "n", get_next_posts_link('Next') );
 
    echo '</ul></div>' . "n";
 
}

0
Jason 2 months 0 Answers 7 views 0

Leave an answer