Support

Account

Home Forums General Issues Making customfields searchable? Reply To: Making customfields searchable?

  • Hi, I’ve just got some more grey hairs and got the code from the Relevassi blog post about this working so it will use custom fields in it’s excerpt. With this code and “custom search result snippets” turned on, it will highlight the search term.
    Hope this helps others!!

    add_filter('relevanssi_excerpt_content', 'custom_fields_to_excerpts', 10, 3);
    function custom_fields_to_excerpts($content, $post, $query) {
    
    $custom_field = get_post_meta($post->ID, 'normal_custom_field', true);
    $content .= " " . $custom_field;
    $custom_field = get_post_meta($post->ID, 'normal_custom_field2', true);
    $content .= " " . $custom_field;
    
    if($fields){
    foreach($fields as $custom_field){
    $content .= " " . $custom_field['repeater_sub_field_1'];
    $content .= " " . $custom_field['repeater_sub_field_2'];
    }
    }
    $fields = get_field('page_content', $post->ID);
    if($fields){
    foreach($fields as $custom_field){
    
    if($custom_field['acf_fc_layout'] == 'layout_name_1'){
    $content .= " " . $custom_field['sub_custom_field'];
    $content .= " " . $custom_field['sub_custom_field2'];
    
    } elseif ($custom_field['acf_fc_layout'] == 'layout_name_2'){
    $content .= " " . $custom_field['sub_custom_field'];
    }
    }
    }
    return $content;
    }