Overview

FacetWP Search facetAllow users to filter content by keyword(s).

Available options

Name Description
Search engine Which search engine to use (extra engines appear if SearchWP or our Relevanssi integration add-on is installed).
Placeholder text The placeholder text that appears within the input box (default: “Enter keywords”). Note: this text is translatable with the facetwp_i18n hook. The default text is also translatable with a translation plugin or a WordPress gettext filter (see this example).
Auto refresh Whether to automatically refresh the results while typing.

What gets searched and how?

By default, Search facets use WordPress core search. For more flexibility, you can use FacetWP’s SearchWP or Relevanssi integration.

WordPress core search is pretty limited: it searches the post title, excerpt, and post content, but nothing else. It has no understanding of relevancy, and no search logic options: it matches search terms with AND logic.

You can exclude search terms though, by prepending them with a - sign. For example, searching apples -pears will result in posts that match apples but not pears. The - exclusion sign can be customized with the wp_query_search_exclusion_prefix hook.

Also good to know is that WordPress search has a default list of so-called “stopwords” that will be ignored. At the time of writing, this list is:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

// See get_search_stopwords() in: includes/class-wp-query.php _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www', 'Comma-separated list of search stopwords in your language' );

This list is a gettext (__()) translatable string, making it possible to set another set of stopwords in your site’s language. You can also add or remove stopwords from the stopwords array (which is constructed from the above string), with the wp_search_stopwords hook.

Using SearchWP or Relevanssi

If you need more flexibility, FacetWP integrates with both SearchWP (built-in) and Relevanssi (with an add-on). Both plugins let you search other data (custom fields, taxonomy terms, PDF content, etc) too. They each offer many settings and add-ons to make search results more relevant, change the search logic (e.g. to OR logic), and enable quoted/exact search or boolean search.

After installing SearchWP, or Relevanssi and the Relevanssi add-on, the available new engine choices will appear in Search facets’ Search engine setting.

Limit the number of results

By default, a Search facet returns a maximum of 200 results. This maximum can be changed by using the facetwp_search_query_args hook to change the posts_per_page query parameter:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_search_query_args', function( $search_args, $params ) { $search_args['posts_per_page'] = -1; // Show all search results return $search_args; }, 10, 2 );

Include draft posts

Similar to the previous example, you can force draft posts to appear in the search results by adding them to the post_status query argument with the facetwp_search_query_args hook:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_search_query_args', function( $search_args, $params ) { $search_args['post_status'] = [ 'publish', 'draft' ]; return $search_args; }, 10, 2 );

Exclude posts from search results

The same hook can be used to add a post__not_in query argument to prevent certain posts from showing up in the search results:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_search_query_args', function( $search_args, $params ) { $search_args['post__not_in'] = [ 893,894 ]; // The post ID(s) to exclude from the search results return $search_args; }, 10, 2 );

Disable order by relevance

When a Search facet is in use, the results are automatically ordered by relevance (using 'orderby' => 'post__in'). To disable this feature and restore the original order, add the following to your (child) theme’s functions.php:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_use_search_relevancy', '__return_false' );

Add a “Clear” button to a Search facet

How to add a clear input icon to a Search facetCurrently, Search facets do not have a “Clear” button or icon.

If you want to add a clear button/icon to a Search facet’s input box, check out our tutorial on how to accomplish that.

Search facets and WooCommerce Catalog visibility settings

WooCommerce 'Catalog visibility' settings.
WooCommerce ‘Catalog visibility’ settings.

If you have a Search facet on a template that queries WooCommerce products, and you are using the “Catalog visibility” settings to influence if products show up in the search results or not, be aware that these settings are ignored when using a Search facet.

If you need these settings to work for a Search facet results, read this guide.

See also