How to combine facet sources
By default, most facet types only support a single data source. However, with a bit of custom code, we can trick FacetWP into indexing multiple data sources, by using the facetwp_index_row hook. With this versatile hook, we can tell FacetWP to index all (or some) values of one (or more) facet(s) into another facet.
Index values of one facet into another facet
For example, let’s assume we want to create an Autocomplete facet with the name full_name
that pulls data from first_name
and last_name
custom fields.
First, create a facet named full_name
and set its data source to the first_name
custom field.
Next, add another facet named last_name
and set its data source to the last_name
custom field.
Finally, add the following code to your (child) theme’s functions.php to trick FacetWP into indexing last_name
values into the full_name
facet. 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
// Replace only 'last_name' and 'full_name' with the name of your facet // Leave $params['facet_name'] intact! add_filter( 'facetwp_index_row', function( $params, $class ) { if ( 'last_name' == $params['facet_name'] ) { // The "source" facet name $params['facet_name'] = 'full_name'; // The "destination" facet name } return $params; }, 10, 2 );
What this code does, is change each post in the indexing table that is being indexed for the last_name
facet as if it was being indexed for the full_name
facet, by switching the facet name while that row is being indexed. The result being that both first_name
and last_name
fields are indexed for the full_name
facet.
Combine values of multiple facets into another facet
If you want to combine multiple facets’ data sources into one new facet, the principle is the same as in the above example. Add the “source” facets’ names to the array in line 2, and change the name of the “destination” facet in line 3.
Make sure the destination facet exists first. Its data source needs to be set to a custom field that does not return any values. Or you can use one of the “source” facets, and index the other ones into it, like in the above example.
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 ( in_array( $params['facet_name'], ['my_source_facet_1', 'my_source_facet_2', 'my_source_facet_3'] ) ) { // Replace the names in the array with your "source" facet names $params['facet_name'] = 'my_destination_facet'; // Replace 'my_destination_facet' with the name of the new combined "destination" facet } return $params; }, 10, 2 );
Combine specific values of multiple facets into another facet
If you want to combine only specific values of one or more facets into another facet, you can use the available $params['facet_value']
to check the value. The following example combines specific values of three different “source” facets into one stock_availability
destination facet.
This example uses the facets’ data source field names (available in $params['facet_source']
) to identify the three “source” facets, instead of their name ($params['facet_name']
, as used in the above examples).
The code also demonstrates how you can change the facet_value
(the technical value as shown in the URL when the choice is selected), and the facet_display_value
(the facet choice label in the facet itself), when these values are stored into the “destination” facet.
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 ) { $source = $params['facet_source']; $value = $params['facet_value']; if ( $source === 'woo/stock_status' && $value == 1 ) { // The "source" facet source and value $params['facet_name'] = 'stock_availability'; // The "destination" facet name $params['facet_value'] = 'home-delivery'; $params['facet_display_value'] = 'Home Delivery'; } if ( $source === 'cf/_variant_instore' && $value === 'yes' ) { // The "source" facet source and value $params['facet_name'] = 'stock_availability'; // The "destination" facet name $params['facet_value'] = 'shop-drive-through'; $params['facet_display_value'] = 'Shop / Drive Through'; } if ( $source === 'cf/_beer_available_in_tap_yard' && $value == 1 ) { // The "source" facet source and value $params['facet_name'] = 'stock_availability'; // The "destination" facet name $params['facet_value'] = 'tap-yard'; $params['facet_display_value'] = 'Tap Yard'; } return $params; }, 10, 2 );