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.

NOTE: In FacetWP 3.5.6 and above, you can choose facet values to include / exclude using the “Value modifiers” UI setting

Exclude certain choices

add_filter( 'facetwp_index_row', function( $params, $class ) {
    if ( 'CHANGE_ME' == $params['facet_name'] ) {
        $excluded_terms = array( '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 = array( '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 = array( 'Featured', 'Reviews' );
        if ( in_array( $params['facet_display_value'], $excluded_terms ) ) {
            $params['facet_value'] = '';
        }
    }
    if ( 'first_name' == $params['facet_name'] ) {
        $excluded_terms = array( 'Bill', 'Mark' );
        if ( in_array( $params['facet_display_value'], $excluded_terms ) ) {
            $params['facet_value'] = '';
        }
    }
    return $params;
}, 10, 2 );