By default, most facet types only support a single data source. With a bit of custom code, we can trick FacetWP into indexing multiple sources, by using the facetwp_index_row indexing hook.

For example, let’s assume we want 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'] ) { $params['facet_name'] = 'full_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.

See also

Last updated: September 10, 2024