Skip to content

Instantly share code, notes, and snippets.

@BrElio
Forked from jenniferhail/wp-tweaks.php
Last active October 22, 2019 13:19
Show Gist options
  • Select an option

  • Save BrElio/36f4f60babbeb760cc14e7568f4ed82e to your computer and use it in GitHub Desktop.

Select an option

Save BrElio/36f4f60babbeb760cc14e7568f4ed82e to your computer and use it in GitHub Desktop.
Index layouts relationship fields for SearchWP
// Adds additional processing for ACF field with the name my_acf_relationship_field
function my_searchwp_acf_relationship_processor( $customFieldValue, $customFieldName, $thePost ){
$content_to_index = '';
if ( 'partner_filter' == $customFieldName ) {
$partner_filter = $customFieldValue[0];
}
preg_match( '/^layouts_\d+_post_type/i', $customFieldName, $matches );
if( 'post_type' == $customFieldName || ( ! empty( $matches ) && is_array( $customFieldValue ) && ! empty( $customFieldValue ) ) ){
$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'
);
}
}
$args['no_paging'] = true;
$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;
wp_reset_postdata();
return $content_to_index;
}
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