It is possible to pre-select facet choices on page load, using the facetwp_preload_url_vars hook.

Let’s pre-select audi in the ‘make’ facet if the current URI is demo/cars and the facet isn’t already in use:

add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
    if ( 'demo/cars' == FWP()->helper->get_uri() ) {
        if ( empty( $url_vars['make'] ) ) {
            $url_vars['make'] = [ 'audi' ];
        }
    }
    return $url_vars;
} );

When a user visits http://yoursite.com/demo/cars/, ‘Audi’ will now be pre-selected in the ‘make’ facet.

Setting the correct value

Depending on the facet type, the value may need to be a multi-element array. Some examples:

// date range
$url_vars['your_date_field'] = [ '2018-01-01', '2018-12-31' ];

// number range
$url_vars['your_number_field'] = [ '25', '500' ];

// proximity (lat, long, radius, label)
$url_vars['your_proximity_field'] = [ '38.9072', '-77.0369', '10', 'Washington%2C%20DC' ];

Pre-select a facet choice for a category, tag, or taxonomy term archive

A common scenario is that you are using a Listing Builder template shortcode on a category, tag, or taxonomy term archive page, and you have a facet with the corresponding category, tag or term as source.

Events category pre-selected in a categories facetWith the facetwp_template_use_archive hook you can automatically pre-filter results based on the current category/tag/taxonomy term. But this hook does not pre-select the facet choice that corresponds to the current category/tag/term archive’s term.

For example, on the category archive /category/events/ you have a facet named ‘categories’ which has category terms as choices, and you want the choice “events” to be pre-selected automatically. Add the following code to your (child) theme’s functions.php to accomplish that:

// Change 'categories' to the name of the categories/terms facet (2x)
add_filter( 'facetwp_preload_url_vars', function( $url_vars ) {
    if ( false !== strpos( FWP()->helper->get_uri(), 'category' ) ) { // change 'category' whatever the URL is for the term archive
        if ( empty( $url_vars['categories'] ) ) { 
            $term = basename( FWP()->helper->get_uri() );
            $url_vars['categories'] = [ $term ];
        }
    }
    return $url_vars;
} );

Note that this code only works when the category/tag/term slug is the same as the facet value (which can be seen in the URL when you select the facet choice: /category/events/?categories=events). The slug also needs to be the last part of the URL.

Pre-select a facet choice after a reset

If you are using a Reset facet, the pre-selected option will be deselected again after a reset.

The following code re-selects that option again when a user clicks the Reset button/link:

<?php
add_action( 'wp_footer', function() {
?>
<script>
document.addEventListener('facetwp-loaded', function() {
    if (! FWP.loaded) {
        FWP.hooks.addAction('facetwp/reset', function() {
            FWP.facets['make'] = ['audi'];
        });
    }
});
</script>
<?php
}, 100 );

See also