Show more posts in search results page

By default, WordPress will show only 10 posts per page while the rest will be accessible on the other pages. Search results respect the same policy and after you hit the search button, you will only get 10 results per page.

Sometimes that’s just not enough and there are times when you will want to display more results per page. Sometimes, it is really much simpler to show 20 or 30 results so your visitors don’t have to keep clicking on the “next” link button. If you want to change the number of posts displayed per page through the entire blog (which will have an effect on the search pages as well), you can do that in the WordPress settings.

In this article, we’re about to show you a simple snippet which will allow you to change only the amount of posts displayed on the search results page while your blogroll will stay intact.

Change the number of posts/pages in search results page:

  1. Open your functions.php file
  2. Copy and paste the following code:
  3. function wpfme_search_results_per_page( $query ) {
    global $wp_the_query;
    if ( ( ! is_admin() ) && ( $query === $wp_the_query ) && ( $query->is_search() ) ) {
    $query->set( 'wpfme_search_results_per_page', 20 );
    }
    return $query;
    }
    add_action( 'pre_get_posts',  'wpfme_search_results_per_page'  );
  4. On the line 4, change the number 20 to any number of posts you want to be displayed on your search results page
  5. Save changes

Now you can open your homepage and test the search engine. If everything’s was alright, you should see up to 20 (or whatever number you have entered) posts on the search results page. Keep in mind that there won’t always be the exact number of search results so even you might have told WordPress to show 100 posts per page, it won’t be able to show them all if there aren’t that many posts to show.

If you want to further improve search results, you should check out how to exclude posts and pages or how to exclude only specific pages from showing in the search results.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

Leave a Reply

Your email address will not be published. Required fields are marked *