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.
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 theme functions.php. Do note that 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) add a new filter hook relevanssi_search_form
which does the same, but only applies to the shortcode search form.
Hi, tried to use the functions.php code above and got an error on the line:
$form = <<<EOH
?
Any ideas?
Thanks
If you copy and paste from the page, looks like extra spaces get included. Remove spaces from the end of all lines.
Hello, how can I make text Search disappears when you click on the container. I put value = Search but this does not go away when you click above must be deleted manually. Thx!!
Here’s how you do that: http://stackoverflow.com/questions/1626107/text-in-html-field-to-disappear-when-clicked
how do you add the custom post type filter to this?
Just add hidden-type input field with the name “post_type” and value that matches the post type.
I have seen this tip at least 10 times now but for some reason it is not working. I am wondering if WP has changed something. For example if I test this on your site https://www.relevanssi.com/?s=download&post_type=page then I am still also getting “posts” in the search results. Any idea why?
Setting “post_type” to “page” has always been unreliable, WordPress silently changes the setting to “any”. That has been the case for years. Instead of “post_type”, use “post_types” to avoid the problem. https://www.relevanssi.com/?s=download&post_types=page works much better.
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!!
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.
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”
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.
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
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.
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.
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.
Just wanted to say thanks– your search is working so much better than the default and what we’ve used before!
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.
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
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.
Thank you! Works Perfect!
Hi, please where I have to put this string (with different value)?
Sandro, put it where you want a search form to appear: in a post, for example.
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)
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.
Is there a way to align the search button with the search field? So far, the search button is located below the field.
Jenn, the code doesn’t do that; it’s up to your theme. Modify the theme CSS to get the desired look for the field.
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
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:
where X is the number of the category.
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!
To restrict to 10 categories, just list all ten categories:
To sort the hanging indexing, try disabling shortcode extension in indexing settings. That probably solves the problem.
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?
Got it! Used the cat ID numbers instead. 🙂 Thanks!
If you want to use the category names, then the parameter is
category_ name
.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?
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:
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.
I took a look at this, and if you’re talking about a forum hierarchy here, then the correct parameter is
post_parent
: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:
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!
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.
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?
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.
Fran, you can copy the default searchform.php from the Twenty Seventeen theme. That’s a good start for the search form. As for the search results template, your theme probably has a Loop template you can use; if not, you can also look at the Twenty Seventeen for an example.
i added the search form shortcode to my site. i want to make the search form taller and longer. How would i do that?
Mark, you can use CSS to style the form any way you want.
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.
Lucas, I’m not sure why the “cats” parameter is not working, but if you change it to just “cat”, looks like that works better.
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,
Yours,
Claudiu
Claudiu, there’s no simple shortcode solution for this, you have to build the meta_query structures yourself. See the WP Codex documentation for meta_query.
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?
Matthew, yes, Relevanssi Premium can serch through all the sites in your multisite.
Can you change the text in the submit search button?
Yes. I’ve added instructions to the post.
Can I use the shortcode of search form to search Tags only?
Frank, if you’re using Relevanssi Premium, [searchform post_types="post_tag"] will do a search form that only returns tag results.
how to find the short code to input in my header please?
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.
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?
Claus, if you set Relevanssi to expand shortcodes, Relevanssi should index the collapsed text.
That is also what I thought. However, for some reason that does not work. Any second thoughts?
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.