Skip to content

Instantly share code, notes, and snippets.

@searchwpgists
Created February 8, 2022 15:01
Show Gist options
  • Select an option

  • Save searchwpgists/1873a29d38c2d1dbff050c97e457ad8f to your computer and use it in GitHub Desktop.

Select an option

Save searchwpgists/1873a29d38c2d1dbff050c97e457ad8f to your computer and use it in GitHub Desktop.
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