Posted on

Using the search form shortcode

Relevanssi includes a search form shortcode that can be used to display a search form. The shortcode is [searchform] and it was introduced in Relevanssi Premium 2.0 and Relevanssi 4.0.

This shortcode prints out a basic search form. If you want to add additional query parameters to it, that’s easy: just add parameters to the shortcode. Here are some examples:

[searchform post_types="product"]
will restrict the search form to just products.

[searchform cats="10,14,17"]
will restrict the search form to categories 10, 14 and 17.

[searchform searchblogs="1,2,3"]
will do a multisite search on blogs 1, 2 and 3.


This function uses the WordPress default search form (provided by get_search_form()). However, if your theme includes a search form (in the searchform.php file), the function will use that. That may cause some unexpected results if your theme search form deviates from the WP standard a lot.

30 comments Using the search form shortcode

  1. Hello, I´m working with ACF and Relevanssi. I created 2 filds:

    [acf field=”locality”]

    [acf field=”city”]

    What I need is the site search form allow search by search: term + city + locality.

    Could you please help me?

    Thank you so much!!

      1. Thank for the quick response! What I need is that you can search by:
        search Term + city + town (3 search fields at the same time and not one). Thanks in advance”

        1. Sure you can do that; it’s just a bit more complicated to do, and doesn’t improve the use experience that much more.

          You’d need to write some code that reads in the city and locality fields and creates a meta_query out of them and adds it to the search parameters.

  2. hi how can i move the search window from it’s defauls place in my template ? also – how do i get relevanssi to bring results from comments on posts ? thanks

    1. I don’t know, the placement of the search box is up to your theme. Relevanssi has nothing to do with it.

      To get results from comments, enable comment indexing in Relevanssi settings and rebuild the index.

  3. Is this what you use to put the actual search bar into your site? Where do you put this to put it in your theme and header? I’d be glad to pay if the search plugin came with a search bar.

    1. I don’t have a search window at all so not sure how to put this in to link to the newly set up Relevanssi.

  4. Just wanted to say thanks– your search is working so much better than the default and what we’ve used before!

    1. Thanks. My commenting system ate your other comments, but no, you don’t need to use this shortcode or pay for anything, you can just use the default WordPress search widget that comes with WordPress.

  5. I added the search form in text editor on a page and it works fab! Im just wondering, is there a way to make the search bar longer? I dont know css or any form of coding, but just used the code to add in text mode on wordpress and it worked a treat. What do i need to add to change the length (make it longer). Thank you very much

    1. Tanya, the lenght of the input field is controlled with a size parameter. Like this:

      <input type="text" value="" name="s" id="s" size="40" />

      Adjust the value to your liking.

  6. Hi Mikko,

    I’ve reviewed your documentation for a couple of hours now, and short of setting up everything on my site, I still have one burning question that remains…

    It appears that all search configuration options apply to sitewide searches. In other words, if I have a primary nav search intended to search the entire site, and I also want to add a search widget to a help / knowledge base page that searches only posts in a particular category, can I differentiate between the two? Is there a way to configure each search form differently to return different results?

    Thanks for your help!

    1. Cat, sure: you can use different search parameters in each form to get different results. For example to restrict a search form to a particular category, add a hidden category variable to the form:

      <input type="hidden" name="cat" value="X" />

      where X is the number of the category.

      1. Ok, thank you! But what if I want to restrict the results to 10 categories? Is it possible to do a check for an array of category values for the input type in a form?

        Now I just need the indexing to complete. I only have around 300 posts and pages but it gets hung up around 30. I’ve let it sit for an hour but nothing happens. I reinstalled the plugin to see if that would fix the hang, but no. Do you know of any reason it would not complete the indexing process? I’m so excited to use this plugin but can’t until I figure that out!

        1. To restrict to 10 categories, just list all ten categories:

          <input type="hidden" name="cat" value="1,2,3,4,5,6,7,8,9,10" />

          To sort the hanging indexing, try disabling shortcode extension in indexing settings. That probably solves the problem.

          1. Thanks! The index hang has been solved with the shortcode extension disabling.

            Still having problems with my form and restricting to specific categories, however. It is probably a simple solution but I’m just not seeing it. Upon running the search, I see the parameters in the search results URL with the search term and the category (trying with one, first) /?s=ads&cat=ads , but it also returns pages on my site that have no category (obviously because they are pages). Why does this happen?

Leave a Reply

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