WordPress Snippets at WPcustoms

Limit search results to show x posts

Change amount of posts shown on the search page – set here to 20


/**
 * Snippet Name: Limit search results to show x posts
 * Snippet URL: http://www.wpcustoms.net/snippets/limit-search-results-to-show-x-posts/
 */
 function wpc_search_results_per_page( $query ) {
  global $wp_the_query;
  if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) {
  $query->set( 'wpc_set_search_results_per_page', 20 );
  }
  return $query;
}
add_action( 'pre_get_posts',  'wpc_search_results_per_page'  );