Overview

With this hook you can pre-select facet choices on page load.

Parameters

  • $url_vars | array | An associative array of facets and their active choices (see below)

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

$url_vars = [ 'my_facet_one' => [ 'choice_one', 'choice_two', // .. ], 'my_facet_two' => [ 'choice_one', 'choice_two', 'choice_three', // .. ], // ... ];

Usage

For example, to pre-select audi in the ‘make’ facet if the current page URI is demo/cars and the facet isn’t already in use, add the following code to your (child) theme’s functions.php:

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_filter( 'facetwp_preload_url_vars', function( $url_vars ) { if ( 'demo/cars' == FWP()->helper->get_uri() ) { // Replace 'demo/cars' with the URI of your page (everything after the domain name, excluding any slashes at the beginning and end) if ( empty( $url_vars['make'] ) ) { // Replace 'make' with the name of your facet $url_vars['make'] = [ 'audi' ]; // Replace 'audi' with the facet choice that needs to be pre-selected. Use the technical name/slug as it appears in the URL when filtering } // add more facet selections if ( empty( $url_vars['vehicle_type'] ) ) { // Replace 'vehicle_type' with the name of your facet $url_vars['vehicle_type'] = [ 'coupe' ]; } } return $url_vars; } );

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

For more information and code examples of how to use this hook exactly in all situations, please refer to our tutorial on how to pre-select facet choices.

See also

Last updated: December 16, 2024