→ 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 ( 'categories' == $params['facet_name'] ) {
        $excluded_terms = array( 'Featured', 'Reviews' );
        if ( in_array( $params['facet_display_value'], $excluded_terms ) ) {
            return false;
        }
    }
    return $params;
}, 10, 2 );

Returning false 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 ( 'categories' == $params['facet_name'] ) {
        $included_terms = array( 'Featured', 'Reviews' );
        if ( ! in_array( $params['facet_display_value'], $included_terms ) ) {
            return false;
        }
    }
    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 ( 'categories' == $params['facet_name'] ) {
        $excluded_terms = array( 'Featured', 'Reviews' );
        if ( in_array( $params['facet_display_value'], $excluded_terms ) ) {
            return false;
        }
    }
    if ( 'first_name' == $params['facet_name'] ) {
        $excluded_terms = array( 'Bill', 'Mark' );
        if ( in_array( $params['facet_display_value'], $excluded_terms ) ) {
            return false;
        }
    }
    return $params;
}, 10, 2 );