Posted on

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.

Adding query parameters

This shortcode prints out a basic search form. If you want to add additional query parameters, 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 post_types="post,page,news"]
will restrict the search form to posts, pages and news.

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

Any parameter you add will be added to the search form directly as a hidden input field.

Adding dropdowns

The shortcode also supports adding dropdown selections for users. You can do dropdowns for post types and taxonomies.

[searchform dropdown="post_type"]
will create a dropdown for post types in the index.

[searchform dropdown="category"]
will create a dropdown for categories.

[searchform dropdown="post_tag"]
will create a dropdown for tags.

All other taxonomies are also possible. You can add multiple dropdowns by using different names for the attributes. As long as the attribute name begins with “dropdown”, it will create a dropdown in the form:

[searchform dropdown_1="post_tag" dropdown_2="category"]


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 unexpected results if your theme search form greatly deviates from the WP standard.

Modifying the search form

If you want to make changes to the search form, you can either use a searchform.php file in your theme to provide a custom search form or use the get_search_form filter function to edit the default search form as you wish. For example, if you want the “Search” button to say “Find”, you can change it like this:

add_filter( 'get_search_form', 'rlv_modify_search_form' );
function rlv_modify_search_form( $form ) {
	$form = str_replace( 'value="Search"', 'value="Find"', $form );
	return $form;

Add this code to your site. Note this will then apply to all search forms, not just the ones generated by the shortcode. Versions 2.2.4 (Premium) and 4.1.3 (free) added a new filter hook, relevanssi_search_form, which does the same but only applies to the shortcode search form.

84 comments The search form shortcode

  1. Hi, tried to use the functions.php code above and got an error on the line:

    $form = <<<EOH


    Any ideas?


  2. 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. Nothing in the search form is necessary, just make Relevanssi index custom fields “locality” and “city”, and searches will find posts by locality and city.

      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.

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

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

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

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

          1. I mean: I have (for the short code) in my text editor this string “[searchform]” and I have now to change the value of the search bar longer; where I have to put the string “” (different size then 40 of course)

          2. Sandro, ah, I see – or actually don’t, because you are trying to tell me something WordPress is removing from the comment. You can’t adjust the length of the search form from the shortcode, you need to do it from CSS.

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

  8. I’m using Relevanssi with bbPress, and would like to create a search form that searches only in specific Forums. As an example, we have several public-facing categories for users, then we have “Staff” forums that are all under a single category. I’d like to be able to have a search form that only searches forums/topics in the Staff section. Is this doable?

    1. Relevanssi and bbPress is not a good match, but if those forums are in a WordPress category, setting a category restriction should work just fine:

      [searchform category_name="staff"]
      1. Mikko,
        I have to disagree with your statement. Relevanssi makes bbPress so much better that I was planning to switch from bbPress to alternate stand-alone forums package and embed until I got Relevanssi setup. Once I added the code you’ve supplied in posts to work with bbP Private Groups and to merge topics and replies in search results, I’m a happy camper. You’ve done an excellent job, and I plan to go premium very soon.

        That said, I’ve never run across the ability to assign a category to a forum, unless we’re using different words for the same meanings. I assume you’re talking about WordPress’s built-in category system and not bbPress categories. If I’m wrong, and the shortcode you provided will limit search to bbPress parent categories, then I’ll be an even happier camper.

        Thank you for your work on Relevanssi, and your response.

        1. I took a look at this, and if you’re talking about a forum hierarchy here, then the correct parameter is post_parent:

          [searchform post_parent="X"]

          Where X is the parent forum post ID. This may not work directly; if the filter does nothing, you need to add this to your theme functions.php:

          add_filter( 'query_vars', 'rlv_qv' );
          function rlv_qv( $qv ) {
          	$qv[] = 'post_parent';
          	return $qv;
          1. That would almost work, but I think the shortcode isn’t going to cut it and I’m going to have to build a custom search form. Here’s some example forum structire:
            – Root
            |- Public Forums
            |–Public Forum 1
            |–Public Forum 2
            |-Staff Forums
            |–Staff Forum 1
            ‘–Staff Forum 2

            So, I could use that shortcode to search all of, say, “Staff Forum 1” or “Public Forum 2”, but not search for posts in “Staff Forums”, if I’m thinking clearly. I suppose I could concatenate the forum IDs for post_parent.

            Thought it definitely seems like a custom form to add the advanced search functionality is what I’ll have to do rather than spend a lot of time setting up a bunch of different forms. Don’t suppose you have any plans to implement such a feature in Relevanssi that would allow users to search on the frontend by selecting categories in a dropdown, by post date, or by author?

            Thanks for all of your help!

          2. Anthony, if you list the post ID for “Staff forums” in the form, the search should include posts in both staff forums under it – listing a parent ID will include all children, including grandchildren.

            This shortcode is as much of a form builder Relevanssi is going to get, I think, for now at least.

          3. I must be doing something wrong here. Here’s my shortcode:
            [searchform post_parent=”25,16,18,15″]

            When I search for, say, “dog” it returns posts outside of those parents. So I added the code you posted above and searches return the homepage instead of search results. I then changed the add_filter line assuming there was a mistake with the name of the callback to:
            add_filter( ‘query_vars’, ‘rlv_qv’ );

            That resulted in once again receiving search results, but they are still not limited to the parents defined in the post_parent parameter of the shortcode.

            What am I missing?

  9. My site does not contain a search for wordpress content at all. If I install relevanssi how do I create a search page? I tried just inserting the shortcode, but it doesn’t create a form.

  10. Hello,
    I am having an issue getting this to work on my site. I am using the following shortcode:

    [searchform cats=”32″]

    I have attempted to remove my search.php template, but my search results are not filtering by category.

  11. Hi there,
    I’m using ACF 4412. I have created a custom fields group called “AAA” with 8 custom fields inside (a1, … a8). I could not find any shortcode to search and filter the whole group nor the custom fields separatelly using Relevanssi 4011. I’d be much obliged if you could point me to the right documentation or to ofer me a soultion.
    Many thanks for you time,
    Looking forward to hear news from you,

  12. Hello,
    I have a custom header I created for my multisite with my search form located there in the header.php in some html and javascript code. I’m trying to get my search to search all sites. Will the plugin work in this case?

    1. Matt, the search form shortcode is [searchform], like described on the page. But in your site header, I would recommend just adding the search form in the template without a shortcode, it’s cleaner and easier that way.

  13. I am using collaps o matic for an FAQ and then I wanted to use Relevanssi to make the collapsed text searchable. No matter how I index I cannot search the collapsed text. What can I do?

        1. Not really, at least without knowing more about your case. When I tested this on my test blog, it just worked, so I have no idea why it doesn’t in your case. It may depend on what kind of content you have in the collapse.

          One thing to check is to see if your site is actually using Relevanssi. Try the Relevanssi admin search (Dashboard > Admin search), that’s guaranteed to be Relevanssi.

  14. Hello,
    I have the shortcode as follows on my “blog” page:

    [searchform post_types=”post”]

    Yet when I type something into the search bar, it shows results that are products or pages along with blog posts. I want it to only search posts in this instance. I haven’t disabled “pages” and “products” from indexing in the relevanssi settings because I want my main search bar, which is in the header, to display all results.

    1. Victoria, if the post_types parameter doesn’t work, then there’s something on your site that is blocking Relevanssi from working, or is overriding the parameter. If you try the Relevanssi admin search (Dashboard > Admin search) and use a “post” as the post type there, does the search work correctly?

  15. Hi Mikko
    Working on ecommerce site.
    I Installed new search plugin name “Advanced woocommerce search”
    with having shortcode ‘aws_get_search_form’.
    i just want to edit above code in my search.php code but couldn’t find where.
    here is my default php code.

    <form class="navbar-search" method="get" action="” autocomplete=”off”>

    <input type="text" id="search" class="form-control search-field product-search-field" dir="” value=”” name=”s” placeholder=”” autocomplete=”off” />

    ‘taxonomy’ => ‘product_cat’,
    ‘hide_if_empty’ => 1,
    ‘name’ => ‘product_cat’,
    ‘selected’ => $selected_cat,
    ‘value_field’ => ‘slug’,
    ‘class’ => ‘postform resizeselect’
    ) ) );

    <form class="navbar-search" method="get" action="” autocomplete=”off”>

    <input type="text" id="search" class="search-field form-control" dir="” value=”” name=”s” placeholder=”” autocomplete=”off” />

    thanks in advance.:)

  16. Hello.
    My theme is orbital theme. I would like to know what code should I use so that relevanssi appears below the search results, since it does not appear.
    I don’t understand much of the code but I guess I should insert it into search.php.
    If possible I would like the search engine at the top to have the word “Search” and on the right, “Dreams”.

      1. Hello.
        my page is a dictionary, and when I search for a word, below the results, I would like to insert the search engine for relevanssi. I am using google translate as my english is not very good. I mean when you search for something on my page, it does not appear relevanssi below the search results. I hope you understand me. Thanks.

        1. Javier, you don’t want to insert Relevanssi, you want to insert a search form. That’s something your theme does, not Relevanssi. If you have a Premium theme, please ask your theme support for how to add a search form to the search results page.

  17. Hi!

    Could I create a search form with an input to exclude words? Something like:

    input #1: introduce words to search {for example: I want to search the text “cars”}
    input #2: introduce words to exclude from search {for example: Exclude the text “bmw”}
    button search

    The search result would be all cars except bmw’s car.

    Thanks and regards!

    1. Javier, yes, this is possible, but requires Relevanssi Premium. The free version of Relevanssi does not support the NOT operator required here.

      In Premium, you’d do this with a function that reads in the search terms in the input #2, adds a minus sign before each word in the input #2 and then adds them to input #1. Something like this:

      add_filter( 'relevanssi_modify_wp_query', 'rlv_exclude_input' );
      function rlv_exclude_input( $query ) {
        $exclude_field = $_REQUEST['exclude'];
        $exclude_words = explode( ' ', $exclude_field );
        $exclude_words = array_map(
          function ( $word ) {
            return '-' . $word;
        $exclude_words_string = implode( ' ', $exclude_words );
        $query->set( 's', $query->query_vars['s'] . ' ' . $exclude_words_string );
        return $query;

      That’s untested and probably doesn’t do everything it should, but at least that gives the basic idea.

  18. When I use the following shortcode in a blank page [searchform dropdown=’post_type’] the dropdown only has one option – “None”.

    Query Monitor has the following 4 errors:

    Trying to get property ‘term_id’ of non-object wp-includes/class-wp-walker.php:136
    Trying to get property ‘name’ of non-object wp-includes/class-walker-category-dropdown.php:61
    Trying to get property ‘term_id’ of non-object wp-includes/class-walker-category-dropdown.php:69
    Trying to get property ‘term_id’ of non-object wp-includes/class-walker-category-dropdown.php:72

    Any idea what is wrong? Are these shortcodes only available with a Premium License?

    1. Nick, no, the shortcode works fine with the free version. The problem is that I’ve been too quick in updating the instructions: the post_type dropdown isn’t available yet, it’s coming in the next version. If you want it now, you can replace the /lib/shortcodes.php file in Relevanssi with this one and then it’ll work.

      1. Thanks! The dropdown works now, however the search results dont work – no matter which type I select, it adds a parameter “&post_type=%24type”. I believe that the problem is on line 123 – swap the quotation marks from ” to “”

          1. Great.

            One question/suggestion: is it possible to select/filter which post types are available within the dropdown? I’d specifically like to use this with a bbpress forum and want to allow the user to select between entire sire (no filter), discussion topics and replies. Or is there another mechanism for this whereby I could insert a shortcode into the page?

          2. Nick, if you want just specific post types in the list, it’s best to manually create the search form. There are many ways you can do that. If you need it to be a shortcode, one easy way is to use the post_type dropdown to create a search form with all post types included, then copy the search form HTML, remove the unwanted post types and then create a new shortcode that will print out that search form.

            add_shortcode( 'mysearchform', 'rlv_mysearchform' );
            function rlv_mysearchform() {
            add search form HTML here

  19. Hi – Wondering if you can restrict the category dropdown to specific IDs within the shortcode? I’m trying to restrict the dropdown to just two IDs like this: [searchform dropdown=”category=11,41″]

    Is this possible? Thanks in advance!

    1. Dave, sorry, that’s not possible. The whole form is filtered with `relevanssi_search_form` filter hook, so you can add the category dropdown to it and then use the filter to remove the unwanted categories.

  20. I’ve encountered an issue where my WordPress site lacks a search function for its content altogether. I’m considering installing Relevanssi to address this, but I’m unsure how to go about creating a search page once it’s installed. I attempted to insert the shortcode, but unfortunately, it didn’t generate a search form as expected. Any guidance on how to set up the search page would be greatly appreciated.
    Thank you.

    1. Isha, Relevanssi provides a search engine, but it cannot create the missing search functionality in your theme. The shortcode uses the theme shortcode features as a base (though it should fall back to WP defaults if the theme doesn’t provide one). Your theme needs to have a search results template. You can look at the WP default themes to see how the search results template is done there, it’s the search.php file.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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