Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tell SearchWP to index both value and label from ACF Select field
<?php
// Tell SearchWP to index both value and label from ACF Select field.
add_filter( 'searchwp\source\post\attributes\meta', function( $meta_value, $args ) {
$acf_field_name = 'state'; // ACF Select field name.
if ( $acf_field_name !== substr( $args['meta_key'], strlen( $args['meta_key'] ) - strlen( $acf_field_name ) ) ) {
return $meta_value;
}
if ( ! is_array( $meta_value ) ) {
$meta_value = [ $meta_value ];
}
$acf_field_object = get_field_object( $acf_field_name, $args['post_id'] );
// Append the Select label to the Select value.
if ( isset( $acf_field_object['choices'] ) ) {
foreach ( $meta_value as $key => $val ) {
if ( isset( $acf_field_object['choices'][ $val ] ) ) {
$meta_value[ $key ] .= ' ' . (string) $acf_field_object['choices'][ $val ];
}
}
}
return $meta_value;
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment