1 min read

Paginating Search Result in WordPress

Inject parameter that control pagination of query result so that search.php can display paginated result
Paginating Search Result in WordPress

Reference

function.php

Following script inject a parameter 'posts_per_page' into the search query to generate paginated search result

add_action( 'pre_get_posts', 'my_set_posts_per_page' );

function my_set_posts_per_page( $query ){
  if ( is_admin() ){
    return;
  }

  //Check if this is a search
  if ( isset( $query->query['s'] ) ) {
    $query->set( 'posts_per_page', -1 );
  }
}