Combining Facet Sources

By default, some facet types (such as autocomplete) only support a single data source. However, it’s possible to support extra data sources via a bit of custom code.

add_filter( 'facetwp_indexer_row_data', function( $rows, $params ) {
    if ( 'test_facet' == $params['facet']['name'] ) {
        $post_id = (int) $params['defaults']['post_id'];
        $field_value = get_post_meta( $post_id, 'address', true );
        $new_row = $params['defaults'];
        $new_row['facet_value'] = $field_value; // value
        $new_row['facet_display_value'] = $field_value; // label
        $rows[] = $new_row;
    return $rows;
}, 10, 2 );

In the above example, we’re adding additional data during FacetWP’s indexing process. If the facet is named test_facet, we grab the address custom field’s value and append it to the array of data to be indexed ($rows).