Redirect a search box to a FacetWP results page

The built-in search widget or other basic text input search form can be directed to a facet search results page without using the facet search filter in the form.  This makes it easy to include a simple search box in your header or sidebar throughout the site without loading FacetWP on every page.

You can import the json below in FacetWP -> Settings -> Backup -> Import if you want a jump start on the facet and template we’re creating in the tutorial.

{"facets":[{"label":"Keywords","name":"keywords","type":"search","source":"post_title","placeholder":"Search ...","search_engine":"","auto_refresh":"no"}],"templates":[{"name":"site_search","label":"Site Search","query":"","template":"","layout":{"items":[{"type":"row","items":[{"type":"col","items":[{"type":"item","source":"featured_image","settings":{"image_size":"medium","link":{"type":"post","href":"","target":""},"is_hidden":"","border":{"style":"none","color":"","width":{"unit":"px","top":0,"right":0,"bottom":0,"left":0}},"background_color":"","padding":{"unit":"px","top":0,"right":0,"bottom":0,"left":0},"text_color":"","text_style":{"align":"","bold":false,"italic":false},"font_size":{"unit":"px","size":0},"name":"el-36l12a","css_class":""}},{"type":"item","source":"post_title","settings":{"link":{"type":"post","href":"","target":""},"prefix":"<h2>","suffix":"<\/h2>","is_hidden":"","border":{"style":"none","color":"","width":{"unit":"px","top":0,"right":0,"bottom":0,"left":0}},"background_color":"","padding":{"unit":"px","top":10,"right":0,"bottom":10,"left":0},"text_color":"","text_style":{"align":"","bold":false,"italic":false},"font_size":{"unit":"px","size":0},"name":"el-mijtwn","css_class":""}},{"type":"item","source":"post_excerpt","settings":{"prefix":"","suffix":"","is_hidden":"","border":{"style":"none","color":"","width":{"unit":"px","top":0,"right":0,"bottom":0,"left":0}},"background_color":"","padding":{"unit":"px","top":0,"right":0,"bottom":0,"left":0},"text_color":"","text_style":{"align":"","bold":false,"italic":false},"font_size":{"unit":"px","size":0},"name":"el-9p7pa","css_class":""}}],"settings":{"border":{"style":"none","color":"","width":{"unit":"px","top":0,"right":0,"bottom":0,"left":0}},"background_color":"","padding":{"unit":"px","top":0,"right":0,"bottom":0,"left":0},"text_color":"","text_style":{"align":"","bold":false,"italic":false},"font_size":{"unit":"px","size":0},"name":"el-68avy","css_class":""}}],"settings":{"grid_template_columns":"1fr","border":{"style":"none","color":"","width":{"unit":"px","top":0,"right":0,"bottom":0,"left":0}},"background_color":"","padding":{"unit":"px","top":0,"right":0,"bottom":0,"left":0},"text_color":"","text_style":{"align":"","bold":false,"italic":false},"font_size":{"unit":"px","size":0},"name":"el-vj6mu","css_class":""}}],"settings":{"num_columns":2,"grid_gap":20,"custom_css":"","border":{"style":"none","color":"","width":{"unit":"px","top":0,"right":0,"bottom":0,"left":0}},"background_color":"","padding":{"unit":"px","top":0,"right":0,"bottom":0,"left":0},"text_color":"","text_style":{"align":"","bold":false,"italic":false},"font_size":{"unit":"px","size":0},"name":"el-k3g93n","css_class":""}},"query_obj":{"post_type":[],"posts_per_page":10,"orderby":[],"filters":[]},"modes":{"display":"visual","query":"visual"}}]}

1. Create a search facet

Although the search widget does not need to be a facet, you will need to have a search facet on the results page.  We’ve created a search type facet with “keywords” as the name.

2. Results template

For this tutorial we’re going to create a FacetWP template with the layout builder.  However, you can also direct the search to an existing archive page (such as a Woocommerce shop home page or blog index) or custom query page template.

Create a new template in FacetWP, we’re labeling this one “Site Search”, which gives the template name as “site_search” to use in the shortcode later.

In the query tab, we’re just going to stick with search all posts but this could be set to specific post types or other parameters if you prefer.

In the layout builder, we’ve created a 2 column display to output featured image, post title, and excerpt.

3. Add a new page

Now create a new page and add the shortcodes to create a results page.  Ours is named “Site Search” and gets a permalink of “/site-search/”, which we’ll need to know to modify the search widget.

Publish this page and test the search to make sure it is working as expected.  Note the url that this creates with the facet in it.  Ours becomes “fwp_keywords”.

The prefix may be “_” or “fwp_” depending on your settings in Settings -> FacetWP -> Settings -> URL Prefix

4.  Search box

We’re using the default WordPress widget for our search.

By default, this widget has a text input field with name “s” and a form action that submits to your home page.  We can modify this with the get_search_form filter.  This will replace the “s” input field name with “fwp_keywords” and change the form action to “/site-search/” to match our new search results page.

/** filters search form to replace the s input with your search facet - change fwp_keywords as needed
 ** and replace the default action with the page url of your custom search results page - change /site-search/ as needed
 ** may need adjusting if the search is already modified by another plugin or theme */
add_filter( 'get_search_form', function( $form ) {
	$form = str_replace( 'name="s"', 'name="fwp_keywords"', $form );
	$form = preg_replace( '/action=".*"/', 'action="/site-search/"', $form );
	return $form;
} );

Now the search box will submit to your new site search results with the keywords in the url.

If the above filter does not work to apply the changes, your theme or another plugin may already be modifying the search form.  Another way to modify the search form is to use the searchform.php template in your theme.  Copy the template from your parent theme and change the input field and form action as above.

You could also create your own form with HTML or a form builder.  The basic form is

<form action="/site-search/"  method="get">
	<input type="search" placeholder="Search &hellip;" value="" name="fwp_keywords">
	<button type="submit">Search</button>