Skip to content

Instantly share code, notes, and snippets.

@mgibbs189
Last active October 20, 2018 07:19
Show Gist options
  • Save mgibbs189/6f4f98754a7c8ff5112b to your computer and use it in GitHub Desktop.
Save mgibbs189/6f4f98754a7c8ff5112b to your computer and use it in GitHub Desktop.
FacetWP - index multiple field values for a single autocomplete facet
<?php
// Add to functions.php
function fwp_combine_sources( $params, $class ) {
if ( 'MY_FACET_NAME' == $params['facet_name'] ) {
$value = get_field( 'YOUR_FIELD_1' );
$params['facet_value'] = sanitize_title( $value );
$params['facet_display_value'] = $value;
$class->insert( $params );
$value = get_field( 'YOUR_FIELD_2' );
$params['facet_value'] = sanitize_title( $value );
$params['facet_display_value'] = $value;
$class->insert( $params );
return false; // prevent default indexing
}
return $params;
}
add_filter( 'facetwp_index_row', 'fwp_combine_sources', 10, 2 );
@barrettcox
Copy link

barrettcox commented Jul 20, 2018

Thanks so much for this! FYI, I ran into an issue where get_field() was returning a NULL value inside the filter function. Eventually, I realized that I needed to grab the post ID from the params array, like this:

$value = get_field('YOUR_FIELD_1', $params['post_id']);

That solved my issue and it worked great after that. Hope that helps anyone who was experiencing the same issue.

@elvismdev
Copy link

@mgibbs189 I'm coming from this filter example for facetwp_indexer_post_facet and now I wonder, isn't that one better performing for this purpose? At least to avoid querying taxonomies, post types or custom fields values that anyways are not going to be used by this facet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment