How to include / exclude facet choices
You may occasionally need to exclude certain choices from a facet, or perhaps even include only certain choices. We’ll cover both approaches below.
Exclude certain choices
To exclude certain choices, add the following code to your (child) theme’s functions.php, and re-index afterward.
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_index_row', function( $params, $class ) { if ( 'my_facet_name' == $params['facet_name'] ) { // Replace 'my_facet_name' with the name of your facet $excluded_terms = [ 'Featured', 'Reviews' ]; // Replace with the display value (not the slug) of your excluded terms if ( in_array( $params['facet_display_value'], $excluded_terms ) ) { $params['facet_value'] = ''; } } return $params; }, 10, 2 );
Setting $params['facet_value']
to an empty string prevents FacetWP from indexing the current value. You could also replace facet_display_value
with facet_value
to target the raw value.
Include only certain choices
To include certain choices, add the following code to your (child) theme’s functions.php, and re-index afterward.
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_index_row', function( $params, $class ) { if ( 'my_facet_name' == $params['facet_name'] ) { // Replace 'my_facet_name' with the name of your facet $excluded_terms = [ 'Featured', 'Reviews' ]; // Replace with the display value (not the slug) of your excluded terms if ( ! in_array( $params['facet_display_value'], $included_terms ) ) { $params['facet_value'] = ''; } } return $params; }, 10, 2 );
Combine multiple rules
To apply rules to multiple facets, just use additional if
statements.
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_index_row', function( $params, $class ) { if ( 'my_facet_name' == $params['facet_name'] ) { // Replace 'my_facet_name' with the name of your facet $excluded_terms = [ 'Featured', 'Reviews' ]; // Replace with the display value (not the slug) of your excluded terms if ( in_array( $params['facet_display_value'], $excluded_terms ) ) { $params['facet_value'] = ''; } } if ( 'first_name' == $params['facet_name'] ) { // Replace 'first_name' with the name of your facet $excluded_terms = [ 'Bill', 'Mark' ]; // Replace with the display value (not the slug) of your excluded terms if ( in_array( $params['facet_display_value'], $excluded_terms ) ) { $params['facet_value'] = ''; } } return $params; }, 10, 2 );