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.

Troubleshooting

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.

39 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!
    Cat

    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?

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

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

Leave a Reply

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