How to Pre-Select Facet Choices
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:
<?php
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 have a category, tag, or taxonomy term archive page, with a facet that uses the corresponding category/taxonomy as its source. Wouldn’t it be nice if that facet would automatically pre-select the current term’s corresponding choice?

For example, say you have a category archive page /category/events/
, on which you have a facet named ‘categories’ which has categories (technically the ‘category’ taxonomy) as source, and you want the choice “events” to be pre-selected automatically.
In case you are using a Listing Builder template shortcode you may already be using the facetwp_template_use_archive hook to automatically pre-filter results based on the current category/tag/taxonomy term. But also this hook does not pre-select the facet choice that corresponds to the current category/tag/term archive’s term.
With the following code you can pre-select the current term’s corresponding choice in the facet. Adapt the facet name and taxonomy slug, and add it to your (child) theme’s functions.php:
<?php
// 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 main URL slug is for the taxonomy term archive
if ( empty( $url_vars['categories'] ) ) {
$term = basename( FWP()->helper->get_uri() );
$url_vars['categories'] = [ $term ];
}
}
return $url_vars;
} );
Note that this code will only work when the category/tag/term slug (events
in this example) 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 URI.
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 );
If you are using the above code example to pre-select a facet choice for a category, tag, or taxonomy term archive, this will not work because the facet choice to be pre-selected is different on each term archive page. You can use the following code instead:
<?php
add_action( 'wp_footer', function() {
?>
<script>
document.addEventListener('facetwp-loaded', function() {
if (! FWP.loaded && false !== FWP_HTTP.uri.indexOf('category')) { // change 'category' whatever the main URL slug is for the taxonomy term archive
let term = FWP_HTTP.uri.split('/').reverse()[0];
FWP.hooks.addAction('facetwp/reset', function() {
FWP.facets['categories'] = [term]; // Change 'categories' to the name of the categories/terms facet
});
}
});
</script>
<?php
}, 100 );