In some situations you may want to combine two or more facet choices into one choice. This can easily be achieved by using the facetwp_index_row indexing hook.

For example, let’s assume you have a facet with the name fruits which has a list of fruit types as choices. And you want to combine the Oranges, Grapefruits, and Lemons choices into one choice: Citrus Fruits. When this choice is selected, the result should be all posts that have Oranges, Grapefruits, and Lemons as taxonomy term or custom field value.

To accomplish this, add the following code to your (child) theme’s functions.php, and make sure to 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 ( 'fruits' == $params['facet_name'] ) { // Replace only 'fruits' with the name of your facet. Leave $params['facet_name'] intact! $val = $params['facet_value']; if ( 'oranges' == $val || 'grapefruits' == $val || 'lemons' == $val ) { // Replace 'oranges'/'grapefruits'/'lemons' with the facet choices to combine (we use the technical name here). Note that || means OR. $params['facet_value'] = 'citrus-fruits'; // Replace 'citrus-fruits' with the new facet_value (technical name/slug). Don't use spaces or special characters. $params['facet_display_value'] = 'Citrus Fruits'; // Replace 'Citrus Fruits' with the new facet choice's display name. } } return $params; }, 10, 2 );

Important to distinguish here is that each facet choice consists of two values that are indexed and stored in the index table. The facet_value is its technical name (“slug”) as it appears in the URL after filtering. And the facet_display_value is the choice’s display value as it shows up in the facet itself in the front-end.

So in the above example, the facet_value of the newly created facet choice is citrus-fruits, which is appears in the URL after selecting it. And its facet_display_value is Citrus Fruits, which appears as choice in the facet.

Another thing to notice is that in line 3 and 4 of the above example, we are using facet_value to select the choices that will be combined. You could also use facet_display_value here, as it is available in the facetwp_index_row hook’s parameters.

See also