-
-
Save searchwpgists/1873a29d38c2d1dbff050c97e457ad8f to your computer and use it in GitHub Desktop.
Tell SearchWP to index both value and label from ACF Select field
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 | |
| // 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