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
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'CHANGE_ME' == $params['facet_name'] ) {
$excluded_terms = [ 'Featured', 'Reviews' ];
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
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'CHANGE_ME' == $params['facet_name'] ) {
$included_terms = [ 'Featured', 'Reviews' ];
if ( ! in_array( $params['facet_display_value'], $included_terms ) ) {
$params['facet_value'] = '';
}
}
return $params;
}, 10, 2 );
Combining multiple rules
To apply rules to multiple facets, just use additional if
statements.
add_filter( 'facetwp_index_row', function( $params, $class ) {
if ( 'CHANGE_ME' == $params['facet_name'] ) {
$excluded_terms = [ 'Featured', 'Reviews' ];
if ( in_array( $params['facet_display_value'], $excluded_terms ) ) {
$params['facet_value'] = '';
}
}
if ( 'first_name' == $params['facet_name'] ) {
$excluded_terms = [ 'Bill', 'Mark' ];
if ( in_array( $params['facet_display_value'], $excluded_terms ) ) {
$params['facet_value'] = '';
}
}
return $params;
}, 10, 2 );