Last active
October 20, 2018 07:19
-
-
Save mgibbs189/6f4f98754a7c8ff5112b to your computer and use it in GitHub Desktop.
FacetWP - index multiple field values for a single autocomplete facet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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 ); |
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.
@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
I am working on a similar issue trying to add multiple results within a single facet... Did you get a resolution to this?