Pre-select facet choices

You may want to pre-select some facets when a user visits a page.

Selecting facet choices

The code below will pre-select audi if the following 3 conditions are met:

  • It’s the initial pageload
  • No facet permalink (URL) variables exist
  • The current page URI is demo/cars

function fwp_preload_facets() {
(function($) {
    $(document).on('facetwp-refresh', function() {
        if (! FWP.loaded && '' == FWP.build_query_string() && 'demo/cars' == FWP_HTTP.uri) {
            FWP.facets['make'] = ['audi'];
add_action( 'wp_head', 'fwp_preload_facets', 100 );
add_filter( 'facetwp_template_force_load', '__return_true' );

The above code can be added to your (child) theme’s functions.php.

Disable template pre-loading

Notice the very last line. This hook forces FacetWP to refresh the template on initial pageload. Otherwise, FacetWP wouldn’t detect the facet selections until after the first AJAX refresh.