Paginating Search Result in WordPress

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 );
  }
}
Show Comments