Overview

The facetwp-refresh event gets triggered before FacetWP begins the refresh process. It runs before any AJAX is requested, and before the URL hash gets updated, also on the initial page load. This event is useful for modifying any FWP variables before getting sent to the server.

Usage examples

Override a facet value

On refresh, force or override a facet choice:

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_footer', function() { ?> <script> document.addEventListener('facetwp-refresh', function() { FWP.facets['vehicle_type'] = ['car']; // Force a specific value }); </script> <?php }, 100 );

Conditionally reset a facet

On refresh, reset facet A when any choice in facet B is currently selected:

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

// Change 'facet_a' and 'facet_b' to the names of your facets. add_action( 'wp_footer', function() { ?> <script> document.addEventListener('facetwp-refresh', function() { if (FWP.facets['facet_b'].length > 0) { FWP.facets['facet_a'] = []; // Reset this facet } }); </script> <?php }, 100 );

On refresh, reset facet A when facet B is the “active” facet (currently being selected or unselected):

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

// Change 'facet_a' and 'facet_b' to the names of your facets. add_action( 'wp_footer', function() { ?> <script> document.addEventListener('facetwp-refresh', function() { if (null !== FWP.active_facet && 'facet_b' == fUtil(FWP.active_facet.nodes[0]).attr('data-name')) { FWP.facets['facet_a'] = []; // Reset this facet } }); </script> <?php }, 100 );

Note that the FWP.active_facet variable is only available during refresh, so within the facetwp-refresh hook.

More examples

See also