Currently, Search facets do not have a “Clear” button or icon.

A (working) Search facet with a “clear” icon.
Updated Sep 10 2024
Updated Sep 10 2024
Updated Nov 12 2024

The following code adds an ‘x’ clear button/icon to the search input box of a Search facet, as you can try out in the working facet on the right.

Clicking the clear icon will automatically reload the (other) facets when clearing the search box, but only if necessary: if a previous search is being cleared.

The code takes the Search facet’s “Auto refresh” setting into account, which makes the result automatically refresh while typing (on keyup, after a slight delay). If this setting is enabled, the clear icon will only appear after the refresh, to prevent users from clearing a running auto-refresh.

Add the following code to your (child) theme’s functions.php. Make sure to replace my_search_facet in line 8 with the name of your Search facet:

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_action( 'facetwp_scripts', function() { ?> <script> (function() { document.addEventListener('facetwp-loaded', function() { const searchfacet = 'my_search_facet'; // Replace 'my_search_facet' with the name of your Search facet const searchbox = document.querySelector(`[data-name="${searchfacet}"] .facetwp-search`); if (!searchbox.nextElementSibling?.matches('i')) { const clearIcon = document.createElement('i'); clearIcon.className = 'clear'; clearIcon.title = 'Clear Search'; searchbox.after(clearIcon); } if (searchbox.value === '') { searchbox.nextElementSibling.style.display = 'none'; } searchbox.addEventListener('keyup', function() { if (FWP.settings[searchfacet]['auto_refresh'] === 'yes') { this.classList.add('loading'); } if (this.value !== '') { this.nextElementSibling.style.display = ''; } }); searchbox.classList.remove('loading'); searchbox.nextElementSibling.addEventListener('click', function() { // ignore while Search facet is loading if (!searchbox.previousElementSibling.classList.contains('f-loading')) { this.style.display = 'none'; searchbox.value = ''; if (FWP.facets[searchfacet].length) { FWP.autoload(); } } }); }); })(); </script> <?php }, 100 );

Next, add the following CSS to your (child) theme’s functions.php. In this example, we are using an ‘x’ image that is already available in FacetWP (in line 15), but you can use any other (font) icon.

The CSS also hides the icon when the facet is loading and when an auto-refresh is ongoing:

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_action( 'wp_head', function() { ?> <style> .facetwp-type-search i.clear { position: absolute; right: 30px; height: 100%; line-height: 1; cursor: pointer; display: inline-block; opacity: 0.3; width: 24px; height: 100%; background: transparent url('/wp-content/plugins/facetwp/assets/images/icon-close.png') 6px 50% no-repeat; background-size: 14px 14px; } .facetwp-type-search i.clear:hover { opacity: 0.8; } /* no clear icon while the Search facet is loading */ /* and when an auto-refresh is running */ .facetwp-type-search .f-loading + input + i.clear, .facetwp-type-search .loading + i.clear { display: none !important; } .facetwp-facet input.facetwp-search { padding-right: 54px; display: inline-block !important; } </style> <?php }, 100 );

See also

Last updated: January 6, 2025