Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active September 18, 2020 12:00
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/159b79962243eb454f2ba6c133c874f5 to your computer and use it in GitHub Desktop.
Save jchristopher/159b79962243eb454f2ba6c133c874f5 to your computer and use it in GitHub Desktop.
Tell SearchWP to index Posts from ACF Relationship Field
<?php
// Customize ACF Relationship field data. By default an array of IDs is stored,
// this customization will instead tell SearchWP to index the full WP_Post object.
add_filter( 'searchwp\source\post\attributes\meta', function( $meta_value, $data ) {
$acf_field_names = [
'acf_relationship_field_name_1',
'acf_relationship_field_name_2',
'acf_relationship_field_name_3',
];
if ( is_array( $data['meta_value'] ) && 1 === count( $data['meta_value'][0] ) ) {
$data['meta_value'] = $data['meta_value'][0];
}
foreach ( $acf_field_names as $acf_field_name ) {
if ( $acf_field_name == $data['meta_key'] ){
$meta_value = array_map( function( $post_id ) {
return is_numeric( $post_id ) ? get_post( $post_id ) : $post_id;
}, $data['meta_value'] );
break;
}
}
return $meta_value;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment