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( 'facetwp_scripts', 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( 'facetwp_scripts', 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( 'facetwp_scripts', 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.

Reset all other facets on change of a specific facet

On refresh, reset all other facets when a specific facet 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 'my_facet_name' to the name of the facet that resets other facets. add_action( 'facetwp_scripts', function() { ?> <script> document.addEventListener('facetwp-refresh', function() { let facet_name = 'my_facet_name'; if ( null !== FWP.active_facet && facet_name == fUtil(FWP.active_facet.nodes[0]).attr('data-name' ) ) { let others = FWP.facets; Object.keys(others).forEach(function (key) { if ( facet_name != key ) { FWP.facets[key] = []; } }); } }); </script> <?php }, 100 );

The above code can be useful to prevent Pager facets from including other facet selections on pages where auto-refresh is disabled.

More examples

See also

Last updated: August 20, 2024