facetwp index multiple values for autocomplete facet

djrmom

{{gist file=”custom-hooks.php” lang=”php”}}
<?php

/**
* add additional fields to an autocomplete factet index
* the datasource for the autocomplete facet is set to post title
* additional custom fields are looked up and inserted during indexing
*/
add_filter( ‘facetwp_index_row’, function( $params, $class ) {

if ( ‘car_search’ == $params[‘facet_name’] ) { // make sure to change ‘car_search’ to the name of your facet

$post_id = $params[‘post_id’]; // get the currrent post id

// copy the params to a new variable so the original is preserved
$new_params = $params;

// add row for make
/** the custom fields for the cars demo are created with Custom Field Suite
** you can grab any value you need from the $post_id
** ex for a custom field – get_post_meta( $post_id, ‘my_custom_field’, true );
** ex for ACF – get_field( ‘my_custom_field’, $post_id );
**/
$value = CFS()->get( ‘make’, $post_id );
$new_params[‘facet_value’] = sanitize_title( $value ); // this is the value for the option
$new_params[‘facet_display_value’] = $value; // this is the keyword(s) that will be search on
$class->insert( $new_params ); // adds the new row to facetwp’s index

// add row for vehicle_type
$value = CFS()->get( ‘vehicle_type’, $post_id );
$new_params[‘facet_value’] = sanitize_title( $value );
$new_params[‘facet_display_value’] = $value;
$class->insert( $new_params );

// add row for driven_wheels
$value = CFS()->get( ‘driven_wheels’, $post_id );
$new_params[‘facet_value’] = sanitize_title( $value );
$new_params[‘facet_display_value’] = $value;
$class->insert( $new_params );

/** $params[‘facet_value’] = ”; // skip indexing, uncomment if you don’t want to index the original datasource **/

}
return $params;
}, 10, 2 );
{{/gist}}