Skip to content

Preselecting a facet and hiding it automatically with FacetWP

September 18th, 2019 WordPress Tips A hiding lion in a field

In this short howto I will show you how you can preselect a facet, and hide it when the page renders.

Preselecting facets

For this case the client has a few facets that are filled by custom fields. The fields are Specialism and Level. These are related to a custom post type Mentor.

Note

When you need to preselect values that contain special characters (tested with ‘ë’, ‘é’, ‘è’) just put a normal ‘e’ in the custom field.

To preselect a facet, we can use the facetwp_preload_url_vars filter. But, during execution the $post global variable is not available, so we need to first get the post. By using the get_uri() method of the FWP->helper class, we can get the uri. This can be mapped to the page name which a separate function nostromo_get_post_id_by_post_name() uses to get the post id.

When we have the post id, it’s possible to get the custom fields and use them as values for the preselects. We just have to add them to the url_vars[] array with the names of the facets.

At the end of the function we return the url_vars array, so the filter can be applied.

Hiding the preselected facet

To hide the preselected facet(s), all we have to is add a piece of CSS to the page, when the url_vars are set. Here’s when a hook comes in very handy. You can see the code on lines 26 and 38. You might need to tweak the CSS, or add some classes to make the CSS specific enough to work. Or use !important, just don’t tell anyone I gave you that advice.

<?
 
/* Facetwp preselect */
add_filter( 'facetwp_preload_url_vars', 'nstrm_filter_facets' );
function nstrm_filter_facets( $url_vars ) {
// get post uri
$uri = FWP()->helper->get_uri();
 
// get post id
$post_id = nostromo_get_post_id_by_post_name( $uri );
 
if ( $post_id ) {
// specialisms
$specialisms = get_field( 'specialisms', $post_id );
 
if ( ! empty( $specialisms ) && ( $specialisms[ 'specialism' ] != '' ) ) {
$specialisms_array = Array();
foreach ( $specialisms[ 'specialism' ] as $specialism ) {
$specialisms_array[] = $specialisms[ 'value' ];
}
 
if ( ! empty ( $specialisms_array ) ) {
$url_vars[ 'specialisms' ] = $specialisms_array;
// Hide this facet, like a lion in the grass
add_action( 'wp_footer', function() { ?>
<style>.facetwp-facet-specialisms { display: none }</style>
&lt;?php } );
}
}
 
// level
$level = get_field( 'filter_level', $post_id );
 
if ( ! empty( $level ) && ( $level[ 'level' ] != '' ) ) {
$url_vars[ 'level' ] = array( $level[ 'level' ] );
// Hide this facet, like a lion in the grass
add_action( 'wp_footer', function() { ?>
<style>.facetwp-facet-level { display: none }</style>
&lt;?php } );
}
}
 
// only return the url vars if they have been filled
if ( ! empty( $url_vars ) ) {
return $url_vars;
}
}
 
?>
php

About Marcel Bootsman

Marcel discovered the web in 1995. Since then he has paid attention to and worked with lots of technologies and founded his own WordPress oriented business nostromo.nl in 2009.

Currently Marcel is Business Development Manager Dutch & DACH Markets at Kinsta where he helps Kinsta's client base grow with Managed WordPress, Application, Database and Static site hosting.

You can contact Marcel on a diverse range of online platforms. Please see the Connect section on the homepage for the details.