Posted on

Changing the number of search results displayed

A standard way to change the number of search results displayed is something like this:

$myquery = "&posts_per_page=5";
$myquery = $query_string.$myquery;
query_posts( $myquery );

Unfortunately, this breaks Relevanssi. Relevanssi hasn’t played nice with posts_per_page in any case, for some reason I don’t really understand — this is legacy code from the original wpSearch plugin.

However, from Premium version 1.4.4 and free 2.9.2, you can now adjust the search results per page in a fairly easy and clean manner. Add this to your site:

add_filter( 'post_limits', 'rlv_postsperpage' );
function rlv_postsperpage( $limits ) {
	if ( is_search() ) {
		global $wp_query;
		$wp_query->query_vars['posts_per_page'] = 10;
	}
	return $limits;
}

Adjust the value (10) the way you want.

Another way to do this:

add_filter( 'relevanssi_modify_wp_query', 'rlv_postsperpage' );
function rlv_postsperpage( $query ) {
    $query->query_vars['posts_per_page'] = 10;
    return $query;
}

47 comments Changing the number of search results displayed

  1. Hi

    I’m using Relevanssi for some of my clients and think it’s a great plugin!
    I use the free version and tried the code above just to see if it worked, but it didn’t. Is there a solution for the free version yet? Is there more code than the snippet above to make this work.
    I’m using free version 2.8.2.

    Kindly,
    Elisabeth

    1. This code doesn’t work in the free version, because it’s something I’ve completely revamped in Premium. I may make the changes in free version as well, possibly in the next version – this is something the free version should have, I think, it’s a fairly basic feature.

      Ah, the post indeed says free will get it in the next version. Let’s make that true, then. This will work in 2.8.3 (or 2.9, actually).

  2. So, is this filter now working with the free version?

    I’ve tried it with 2.9.1, but it dosen’t seem to work properly…

    1. Oops, sorry. Forgot. I’ve been busy, and to be honest, had I waited until I had time to add this to the free version, there wouldn’t be any new versions out yet. This requires a major change in how the plugin works.

  3. Hi, with this now my blog breaks meaning all loops are not limited anymore, no matter if I add the code or not. All loops show now all query results in a single page.

  4. Thanks. Is it possible to paginate this or make that “10” dynamic, according to the number of results returned. This is pretty easy on the page with query_posts but I have to get around that.

    Thanks!

  5. Unfortunately I don’t have pagination when I place this code in. Any suggestions? I can’t just replace 10 with $wp_query->found_posts

  6. Hi

    What do I do if I want to change the no of results dynamically. I.e. If I add a select field named “num_results” with options of value 10,20,50

    $num_results = $_POST[‘num_results’];

    add_filter(‘post_limits’,’postsperpage’,1,$num_results);
    function postsperpage($limits) {

    if (is_search()) {
    global $wp_query;
    $wp_query->query_vars[‘posts_per_page’] = $limits;
    }
    return $limits;
    }
    This just returns the default number pages set in General->Reading for posts

    Thanks

    Matt

  7. One big question.

    I love Relevanssi, really. But there is one thing i do not understand.

    One a website im building i have 4 different post_types. With different content and displaying of pages. So naturally they also have a different post_per_page. For instance;

    -i have a “faq” post_type displaying 10 post a page
    -i have a “projects” post_type tha displays 3 post a page.

    i also have separate search boxes for the post. Now i want the Relevanssi search result to also display 10 results on the “faq” post_type and 3 results on the “projects” post_Type.

    What would be the best method to fix this. I tried to make a new function based on the one above. But this only results in errors.

    Thanks

  8. Something like this should do the trick:

    add_filter('post_limits', 'postsperpage');
    function postsperpage($limits) {
    	if (is_search()) {
    		global $wp_query;
    		$n = 10;
    		if ($wp_query->query_vars['post_type'] == "projects") $n = 3
    		$wp_query->query_vars['posts_per_page'] = $n;
    	}
    	return $limits;
    }
  9. Is there a way to create a unique sidebar for search results? We have a number of Custom Post Types and different sidebars appear depending on results (it seems to depend on what is first result) I would like (after initial search) to have a unique sidebar (and widgets) for all search results. How is this possible?

    1. This depends completely on your theme. How does your theme do sidebars? Can you add conditionals there? There’s is_search() to check if you’re on a search results page and then you can check $wp_query->query_vars for more details, if you want more specific sidebars.

  10. I am building a multi-site multi-language theme for a client.

    They want two types of searches. In the header they want a full site search. On the industry pages (I am using categories for industries) they only want to search a subset of posts, they want to ignore all pages and search only the posts with industry categories.

    Can Relevanssi do this. I have been testing it with the free plug-in deciding if I should make the investment to the pro version. Will this plug-in solve my problem, if yes, how do I set this up?

    Thanks,

    Susan

    1. Yes, this is easy. Just add

      [input type=”hidden” name=”cats” value=”X” /]

      to the industry search form. Replace X with the top industry category ID. That should do the trick, and it also works with the free version.

      Multisite search requires Relevanssi Premium, and doesn’t support category restrictions when searching more than one site.

  11. Adding this to my functions.php (WP 4.2.5) kills my site completely. Should the “add_filter” really be outside the function?

    1. “Limit searches” is a performance-related feature. If you have thousands and thousands of posts, it makes sense to enable that option to make searches run a bit faster. If you don’t have thousands of posts, you don’t need that.

      I’m not sure how Google comes into this, so please elaborate.

  12. Hi,

    My name is Timothy. Thank you for this post. I was curious how to filter the search bar results (in my case to 4-5). Changing the Limit from 500 to 4 works, but, then the actual search results page only shows 4 (whereas I might have 53+ on the search results page). I have uploaded an image in hopes of explaining the search area I am wanting to change the number of results for. Please let me know your thoughts.

    Respectfully,
    Timothy

    https://uploads.disquscdn.com/images/722e1f653d2a9da15060293860d6f44d8875128ca977e0ddcce4bcb694bfb27f.jpg

    1. Do not adjust the “Limit searches” option. It needs to be at least 200-300 for the search to work properly. Setting it to 4 will break the search completely. That feature is not for adjusting the number of search results; that is done with the posts_per_page feature, as described on this page.

      You need to adjust posts_per_page for this particular search. How that is done, I can’t tell, as I don’t know how the search is set up.

      1. I appreciate the quick response, Mikko! I will look into adjusting the posts_per_page. I’m not sure how to do that either, but given the fairly precise “clue” you gave, I believe I can figure it out.

  13. Hi,

    I used the ‘post_limits’ approach to set ‘posts_per_page’ to -1 (show all). One side effect I noticed was that this broke pagination on WP admin dashboard searches – pagination would be visible by default, but would disappear when searching in admin view.

    I solved this by changing `if (is_search())` to `if (is_search() && !is_admin())`. Just sharing this here in case someone else runs into the same problem.

    BR,
    Jari

  14. This plugin is great. I am using this limit code snippet in my functions.php file and it works great. For pagination do I need to use some plugin or there is some additional code snippet available to generate pagination.

    Thanks and looking forward for some quick response.

  15. How can i made three different filters like:
    1) 10 Records
    2) 20 Records
    3) 30 Records

    so that if user click on 10 records then 10 records will be shown in search results and if user select 20 records then it will display 20 records.

    Thanks

    1. For starters, it’s probably better to just show 30 records to start with. As a user, I’m always really annoyed when I’m shown just few records and then have to click to see more. Why not just show more to start with?

      But to create the filter, just create a link that has the same query parameters as the current search results page, but with a different value of posts_per_page.

  16. Yes I have seen that code available in the above link but to make a link with these variable. I tried but failed.

  17. Is this still the best way and does it still work? I’m not having any luck with it at all. I’ve tried modifying the query through WP’s pre_get_posts filter to no avail and then found this but I’m still getting whatever is set in admin.

  18. Hi Mikko,
    Thank you for your wonderful plugin.
    I installed it on my previous site, a couple of years ago, and I got about 30 results on the first page. However, I just installed it on a new site and now I get only one result per page.
    I don’t know php and will not risk using the code you provided.
    Is there another solution? How can I get 30 results per page like my first website?
    Thank you

    1. Marina, your theme settings may be set to show just one post per page. You can adjust that, though it may affect other parts of your site. The code is the safest option. This is a five-minute job for a WP developer, so perhaps you can hire one for cheap to do it for you.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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