HowTo: Change number of posts displayed on Search

WordPress usually use a constant number of posts for all your category pages, index, and also search results pages. For search pages, 10 posts are not usually enough. To change this behaviour, a simple query filter will do the trick.

If you want to change number of posts displayed on search results page, just put that snippet to your functions.php file:

add_action( 'pre_get_posts',  'shailan_change_post_count'  );
function shailan_change_post_count( $query ) {

  global $wp_the_query;

  if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) {
    $query->set( 'posts_per_page', 20 ); // For search pages
  } elseif ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_archive() ) ) {
    $query->set( 'posts_per_page', 20 ); // For archives like tags, category, etc..
  }

  return $query;
}

Don’t forget to change number of posts in the snippet.

(Image credit)