How to add pagination in search results

Viewed 16682

My search.php page looks like this:

get_header(); ?>
<div class="search_result_page">
<section class="content-area">
    <?php
    $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    if ( have_posts() ) : ?>
        <header class="results_heading">
            <h6 class="rsults_title"><?php printf( esc_html__( 'Search Results for: %s', 'satsco' ), '<span class="search_query">' . get_search_query() . '</span>' ); ?></h6>
        </header><!-- .page-header -->
        <div id="result_search">
        <?php
        /* Start the Loop */
        while ( have_posts() ) : the_post();

            get_template_part( 'template-parts/content', 'search' );

        endwhile;?>
        </div>
        <?php
        the_posts_navigation();
    else :
        get_template_part( 'template-parts/content', 'none' );
    endif; ?>
</section><!-- #primary -->
</div>
<?php
get_footer();

Inside the while loop it calls another page called content-search which looks like this?

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
    <?php the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>

    <?php if ( 'post' === get_post_type() ) : ?>
    <?php endif; ?>
</header><!-- .entry-header -->
<div class="entry-summary">
    <?php the_excerpt(); ?>
</div><!-- .entry-summary -->
</article><!-- #post-## -->

I just noticed now that when I search for "Where", it shows me all the posts that have the word Where.

So, how can I add pagination to it?

2 Answers

Base on answer from @elton-sousa iam added this add_action( 'pre_get_posts', 'search_filter' ); in functions.php and work.
all script like below :

function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $query->set('paged', ( get_query_var('paged') ) ? get_query_var('paged') : 1 );
      $query->set('posts_per_page',1);
    }
  }
}
add_action( 'pre_get_posts', 'search_filter' );
Related