Skip to main contentSkip to footer

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

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Elementor

…a Query ID in the Elementor settings and then add this to your site: add_action( ‘elementor/query/YOUR_QUERY_ID’, function( $query ) { $query->set( ‘relevanssi’, true ); } ); This will make that specific query use Relevanssi. Using Relevanssi with Elementor Live Results Elementor has a feature for custom Query Filters. These can……be used to connect Relevanssi with the Elementor Live Results and the Elementor Search Widget. First, you need to add this function to your site: function my_relevanssi_results( $query ) { $query->set( ‘relevanssi’, true ); $query->set( ‘orderby’, ‘relevance’ ); } add_action( ‘elementor/query/my_relevanssi_results’, ‘my_relevanssi_results’ ); Then, follow these instructions from Elementor: Assuming…Elementor is a popular page builder. It works rather well with Relevanssi, most of the time. Check for e_search_props If the search results come back empty or are restricted to a specific post type, it’s possible Elementor is restricting them. Elementor can hide the query parameters to a single query…

Using phrases in Related posts searches

I have a book review site where I’m using Relevanssi Premium and the Related posts to highlight related book reviews. The reviews have multiple taxonomies used for the related posts: tags, book author, series, characters, and places. Messy relations The problem is, with the default settings, the Related posts get…

Indexing attachment file names

…( ‘attachment’ === $post->post_type ) { $content .= ‘ ‘ . basename( $post->guid ); } return $content; } Notice that by default Relevanssi replaces periods with spaces, so “sample.pdf” is indexed as “sample pdf”. That should not be a problem, as also in searching “sample.pdf” becomes “sample pdf”. However, if…

Related Posts:

Comment Section:

49 Comments. Leave new

  • Hi Mikko,

    I’m trying to modify the query. Right now it fetches all records in the background in chunks, but I want to set a limit so that it only fetches 15 per page. I already have pagination, but currently it loads all the data first and then pagination works.

    Here’s my query:
    $search_args = array(
    ‘post_type’ => $arg_post_type,
    ‘post_status’ => ‘publish’,
    ‘posts_per_page’ => 15,
    ‘paged’ => $search_paged,
    ‘relevanssi’ => true,
    ‘s’ => stripslashes($get_search),
    );

    I want it to load only 15 results on the first page, but right now it loads everything in chunks first.

    Reply
    • Chetan, that’s how it works: Relevanssi needs to find all matching posts, sort them and then it can tell which are the fifteen best matches. It can’t just fetch the fifteen best matches because it doesn’t know which posts those are.

      Reply
  • Marina Virgin
    November 7, 2019 9:41 am

    Thank you for the tip.

    Solved!

    Reply
  • Marina Virgin
    November 6, 2019 1:54 pm

    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

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

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

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

    Reply
  • Thanks can you please provide some sample code?

    Reply
  • Tariq Altaf
    March 8, 2019 5:28 pm

    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

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

      Reply
  • Tariq Altaf
    March 7, 2019 6:43 pm

    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.

    Reply
  • Jari Huilla
    May 15, 2017 2:11 pm

    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

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

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

      Reply
      • Timothy Flores
        February 18, 2017 6:32 am

        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.

        Reply
  • 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 http://yellowbric.com/
    and please tell me how about this setting [pic]
    https://uploads.disquscdn.com/images/1e23e3801a1e8f14bb7437fa5ca81971e0bea78fa113cb981187ceb50ab85abb.jpg

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

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

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

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

    Reply
  • Thomas Roberts
    July 30, 2012 7:40 am

    hello Mikko,

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

    Thanks

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

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

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

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

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

    Reply
  • 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;
    }
    Reply
  • 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

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

    Reply
  • Thomas Bennett
    October 20, 2011 3:12 pm

    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

    Reply
  • Thomas Bennett
    October 19, 2011 10:23 pm

    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!

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

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

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

    Reply
  • 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…

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

      Reply
  • Elisabeth F
    May 31, 2011 10:39 am

    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

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

      Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed