Created
September 4, 2019 17:29
-
-
Save jchristopher/ab70357f0f942d9e9f28d4cc3b898cd8 to your computer and use it in GitHub Desktop.
Tell SearchWP to index term names from ACF (Advanced Custom Fields) Taxonomy fields
This file contains 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 | |
// Tell SearchWP to index term names from ACF (Advanced Custom Fields) Taxonomy fields. | |
add_filter( 'searchwp_custom_fields', function( $meta_value, $meta_key, $the_post ) { | |
$acf_taxonomy_field_names = array( 'mytaxfield1', 'mytaxfield2' ); | |
if ( ! in_array( $meta_key, $acf_taxonomy_field_names ) ) { | |
return $meta_value; | |
} | |
// Retrieve the value as per ACF's field settings. | |
$acf_field_object = get_field_object( $meta_key, $the_post->ID ); | |
// We want to index the chosen terms names. | |
$meta_value = wp_list_pluck( $acf_field_object['value'], 'name' ); | |
return $meta_value; | |
}, 20, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment