Last active
September 18, 2020 12:00
-
-
Save jchristopher/159b79962243eb454f2ba6c133c874f5 to your computer and use it in GitHub Desktop.
Tell SearchWP to index Posts from ACF Relationship 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 | |
// 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