Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active November 11, 2016 04:50
Show Gist options
  • Save jchristopher/7880328 to your computer and use it in GitHub Desktop.
Save jchristopher/7880328 to your computer and use it in GitHub Desktop.
Parse Advanced Custom Fields (ACF) Relationship field data and extract appropriate information for SearchWP to index
<?php
// in this case we have three ACF Relationship fields set up, they have the names
// acf_rel_1, acf_rel_2, and acf_rel_3 (NOTE: these are the NAMES not the labels)
// ACF stores post IDs in one form or another, so indexing them does us no good,
// we want to index the title of the related post, so we need to extract it
function my_searchwp_custom_fields( $customFieldValue, $customFieldName, $thePost ) {
// by default we're just going to send the original value back
$contentToIndex = $customFieldValue;
// check to see if this is one of the ACF Relationship fields we want to process
if( in_array( strtolower( $customFieldName ), array( 'acf_rel_1', 'acf_rel_2', 'acf_rel_3' ) ) ) {
// we want to index the titles, not the post IDs, so we'll wipe this out and append our titles to it
$contentToIndex = '';
// related posts are stored in an array
if( is_array( $customFieldValue ) ) {
foreach( $customFieldValue as $relatedPostData ) {
if( is_numeric( $relatedPostData ) ) { // if you set the Relationship to store post IDs, it's numeric
$title = get_the_title( $relatedPostData );
$contentToIndex .= $title . ' ';
} else { // it's an array of objects
$postData = maybe_unserialize( $relatedPostData );
if( is_array( $postData ) && !empty( $postData ) ) {
foreach( $postData as $postID ) {
$title = get_the_title( absint( $postID ) );
$contentToIndex .= $title . ' ';
}
}
}
}
}
}
return $contentToIndex;
}
add_filter( 'searchwp_custom_fields', 'my_searchwp_custom_fields', 10, 3 );
@91design
Copy link

91design commented Nov 11, 2016

I copied this script in my child theme functions.php, replaced the array with my cf names and reindexed the site, but it isn't working. What I'm doing wrong?

This is my code
// in this case we have three ACF Relationship fields set up, they have the names // acf_rel_1, acf_rel_2, and acf_rel_3 (NOTE: these are the NAMES not the labels) // ACF stores post IDs in one form or another, so indexing them does us no good, // we want to index the title of the related post, so we need to extract it function my_searchwp_custom_fields( $customFieldValue, $customFieldName, $thePost ) { // by default we're just going to send the original value back $contentToIndex = $customFieldValue; // check to see if this is one of the ACF Relationship fields we want to process if( in_array( strtolower( $customFieldName ), array( '_products', 'relazionale' ) ) ) { // we want to index the titles, not the post IDs, so we'll wipe this out and append our titles to it $contentToIndex = ''; // related posts are stored in an array if( is_array( $customFieldValue ) ) { foreach( $customFieldValue as $relatedPostData ) { if( is_numeric( $relatedPostData ) ) { // if you set the Relationship to store post IDs, it's numeric $title = get_the_title( $relatedPostData ); $contentToIndex .= $title . ' '; } else { // it's an array of objects $postData = maybe_unserialize( $relatedPostData ); if( is_array( $postData ) && !empty( $postData ) ) { foreach( $postData as $postID ) { $title = get_the_title( absint( $postID ) ); $contentToIndex .= $title . ' '; } } } } } } return $contentToIndex; } add_filter( 'searchwp_custom_fields', 'my_searchwp_custom_fields', 10, 3 );

The cf value is saved in this format a:4:{i:0;s:4:"1244";i:1;s:3:"777";i:2;s:3:"257";i:3;s:5:"11733";}

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