WordPress ACF and Relevanssi custom fields highlighting

<?php 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; }
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!!

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.