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

    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

    • http://www.mikkosaari.fi/ Mikko Saari

      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…

    • http://www.mikkosaari.fi/ Mikko Saari

      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.

  • http://www.mikkosaari.fi/ Mikko Saari

    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.

    • http://www.mikkosaari.fi/ Mikko Saari

      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.

  • http://www.news.cyprus-property-buyers.com/ Nigel Howarth

    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.

    Thanks!

    • http://www.mikkosaari.fi/ Mikko Saari

      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

    • http://www.mikkosaari.fi/ Mikko Saari

      Sorry, no idea then, as that should work.

  • Matt

    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

    • http://www.mikkosaari.fi/ Mikko Saari

      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.

    Thanks

  • http://www.mikkosaari.fi/ Mikko Saari

    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!

  • http://www.rblab.com/blog/ Riccardo Bartoli

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

    Thanks

    • http://www.mikkosaari.fi/ Mikko Saari

      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?

    • http://www.mikkosaari.fi/ Mikko Saari

      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.

  • http://inconstruction Susan Fisher

    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

    • http://www.mikkosaari.fi/ Mikko Saari

      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?

    Thanks

    • http://www.mikkosaari.fi/ Mikko Saari

      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.