Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created April 27, 2021 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/8a68facd0df8fc9d010c8d3d9840a3f0 to your computer and use it in GitHub Desktop.
Save jchristopher/8a68facd0df8fc9d010c8d3d9840a3f0 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 );
@naderjlyr
Copy link

Thanks for this awesome plugins and also code snippets.
Is there anyway to provide us for indexing gallery field custom fields?
I mean, as you may know ACF returns gallery field as an array, actually nested array including all the gallery items.
What I'm trying to do is to index each image title for search engine.
is there anyway you can help me with it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment