Skip to content

Instantly share code, notes, and snippets.

@jenniferhail
Last active October 22, 2019 13:19
Show Gist options
  • Select an option

  • Save jenniferhail/3f255db5a2c08b7bd2c2ec43c9e3d08d to your computer and use it in GitHub Desktop.

Select an option

Save jenniferhail/3f255db5a2c08b7bd2c2ec43c9e3d08d to your computer and use it in GitHub Desktop.
Index layouts relationship fields for SearchWP
function my_searchwp_acf_relationship_processor( $customFieldValue, $customFieldName, $thePost ){
$content_to_index = '';
if ( 'partner_filter' == $customFieldName ) {
$partner_filter = $customFieldValue[0];
}
if ( 'post_type' == $customFieldName ) {
$post_type = $customFieldValue[0];
if ($post_type == 'post') {
// define the query here
$args = array(
'post_type' => 'post'
);
} else if ($post_type == 'muw_program') {
// define the query here
$args = array(
'post_type' => 'muw_program'
);
} else if ($post_type == 'muw_events') {
// define the query here
$args = array(
'post_type' => 'muw_events'
);
} else if ($post_type == 'muw_allies') {
// define the query here
$args = array(
'post_type' => 'muw_allies'
);
} else if ($post_type == 'muw_partners') {
// define the query here
// has option to filter by taxonomy, muw_partner_types
// otherwise, display all
if ( $partner_filter != '' ) {
$args = array(
'post_type' => 'muw_partners',
'tax_query' => array(
array(
'taxonomy' => 'muw_partner_types',
'field' => 'id',
'terms' => $partner_filter
)
)
);
} else {
$args = array(
'post_type' => 'muw_partners'
);
}
}
$query = new WP_Query($args);
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
$listing_title = get_the_title();
$content_to_index .= $listing_title . ' ';
endwhile;
endif;
return $content_to_index;
wp_reset_postdata();
}
preg_match( '/^layouts_\d+_available_content/i', $customFieldName, $matches );
if( ! empty( $matches ) && is_array( $customFieldValue ) && ! empty( $customFieldValue ) ){
// We want to index Titles not IDs
foreach( $customFieldValue[0] as $post_id ){
$post_title = get_the_title( absint( $post_id ) );
$content_to_index .= $post_title . ' ';
}
return $content_to_index;
}
return $customFieldValue;
}
add_filter( 'searchwp_custom_fields', 'my_searchwp_acf_relationship_processor', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment