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;

Unfortunately, this breaks Relevanssi. Relevanssi hasn’t played nice with posts_per_page in any case, for some reasons 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 functions.php file:

add_filter('post_limits', 'postsperpage');
function 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.

  • Elisabeth F


    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.


    • 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).

  • Lore!

    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…

    • 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.

  • Ok, now it’s for real — this’ll appear in the next version of Relevanssi free (2.9.2).

  • Richard

    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.

    • Yes, there was a very small bug in 2.9.2 with surprisingly big consequences. The bug has been fixed, just upgrade to 2.9.3.

  • Thanks for adding this feature to the Relevanssi plugin – it’s a great addition to one of the best plugins around.

  • Thomas Bennett

    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.


    • That code will paginate the results, showing ten results on each page.

      But sure, you can make it dynamic, just add conditionals there as you wish.

  • Thomas Bennett

    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

    • Sorry, no idea then, as that should work.

  • Matt


    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’];

    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



    • Try fetching the number of results from the $_POST inside the filter function.

      $wp_query->query_vars[‘posts_per_page’] = $_POST[‘num_results’];

  • Jasper

    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.


  • 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;
    • Jasper

      This works great! Thanks!

  • I’m trying to show all results but a value of -1 shows no results. Is there a different value I’ve to use?


    • Use something large enough, like 1000000.

  • Peter Ricci

    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?

    • 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.

  • 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?



    • 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.

  • Thomas Roberts

    hello Mikko,

    great plugin, is it possible to jump straight to first search result?


    • I suppose it is, but I can’t come up with an easy way to do it.

  • Manu

    I’ve searched the solution during 3 hours, thanks a lot !

  • user2013

    How do you genrate the results with pagnation? i’ve set it to 100, it displays it all one one page.

  • matturn

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

    • Yes, it must be. Make sure there are no typos in the code.

  • Can you tell me how to make boost setting but not much, or still safe from google. I put this plug in on this site
    and please tell me how about this setting [pic]

    • “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.

  • Timothy Flores


    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.


    • 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.

      • Timothy Flores

        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.

  • Jari Huilla


    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.