X Features Cars Demo Power Plants Demo State Parks Demo Site Showcase Docs Tutorials Add-ons Buy

Pre-select facet choices

Important: this tutorial is aimed at FacetWP 3.0.4 and above.

Let’s pre-select audi if the current page URI is demo/cars and there’s no existing value for it.

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

When a user visits http://yoursite.com/demo/cars/, Audi will be pre-selected 🙂

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'] = array( '2018-01-01', '2018-12-31' );

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

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