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

35 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

Leave a Reply

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