Posted on

How to add custom fields to search excerpts

Getting Relevanssi to index custom fields and find posts by custom field content is easy – just list the custom fields in the appropriate setting and re-index. However, Relevanssi doesn’t include custom field content in the custom search excerpts by default – it doesn’t really know how to do that.

It’s not particularly complicated, though. You can use the relevanssi_excerpt_content to add any content you want to the source material Relevanssi uses to build the excerpts. Adding custom fields is as easy as this:

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, 'custom_field_1', true);
    $content .= " " . $custom_field;
    $custom_field = get_post_meta($post->ID, 'custom_field_2', true);
    $content .= " " . $custom_field;
    return $content;

This would make the contents of the custom fields “custom_field_1” and “custom_field_2” available for Relevanssi excerpt-building. Just add the code to functions.php. There’s no need to rebuild the index after adding this code, you should see the effects immediately.

59 comments How to add custom fields to search excerpts

      1. Hi, is there a way for this to work with all ACF fields? I have so many in a large site that listing them one by one would be massive.

          1. I thought that person couldn’t get it to work. Sorry, I’m not much of a php dev, I’m a front-end guy 😉

      2. Hi, do you have any examples of how you adjust the code to read repeater and flexible content fields? Your example is working well for standard fields but I am getting nothing at all for those.

  1. Hey,
    I have add a custom field of dimension in my product post and want to get search a product by its dimension by its not working can u please help me …im using the relevanssi plugin..

      1. I have use a plugin and its searching a product by its name and contetn..but whenever im going to give the length ,height,width..its not showing any prodcuts on search page like if i type in search box i want a prodcut of dimension like : 40WX10HX10L..the product is there in my site but its not showing me on search page..

        1. add_filter(‘relevanssi_excerpt_content’, ‘excerpt_function’, 10, 3); function excerpt_function($content, $post, $query) {

          global $wpdb; $fields = $wpdb->get_col(“SELECT DISTINCT(meta_key) FROM $wpdb->postmeta”);

          foreach($fields as $key => $field){ $field_value = get_post_meta($post->ID, $field, TRUE); $content .= ‘ ‘ . ( is_array($field_value) ? implode(‘ ‘, $field_value) : $field_value ); }

          $content = preg_replace($wordlist, ”, $content);

          return $content; }

          i have add dis code in my fucntion.php file but its not giving me any output

  2. I have a new site that uses ACF with repeater and flexible content fields and also Relevannsi. The search result excerpts are not good. By default there is no result snippet from the ACF fields. I have tried several methods I’ve found online to improve on this but none are fully successful. As a front end person I am sadly way out of my depth trying to work out a solution for this. ACF is pretty much ubiquitous these days so it is very important to me that I can get good search results and snippets from ACF fields. If anyone has found a good solution I would be v grateful to see it. Thanks!

    1. It gets complicated, and the solutions are going to be unique to your needs. Any ACF-capable developer should be able to help you with this, the Relevanssi side of things is really simple and described in the post above.

      1. Thanks for the ultra quick reply! Would you be able to provide just one hypothetical example, that maybe I could use to start figuring out what I need? For example, I have a flexible content field called “create_panels”, and in it there is a layout called “single-column”, and in that a wysiwyg field called “panel_content” that I would like to be able to show in snippets. If you could tell me how to do that, I could probably use it to sort out all the others. I find that people like me, designers turned front end developers, can generally get on pretty well if they have a few concrete examples to look at. Thanks again.

        1. I’m not an ACF developer, so I don’t know. There’s a concrete example of the Relevanssi side above: you need a function on relevanssi_excerpt_content hook that reads the ACF fields and returns their content, and then it’ll be used in the excerpt-building.

          How do you do that reading of ACF fields is easy for any ACF-capable developer, but I don’t know how that happens as I don’t use ACF.

          1. That makes two of us! I have already posted on ACF as well, so I’ll see if they have any suggestions. Thanks.

          2. I got this from ACF support:

            foreach($fields as $field){
            $content .= ” ” . $field[‘repeater_sub_field_1’];
            $content .= ” ” . $field[‘repeater_sub_field_2’];
            $fields = get_field(‘flexible_field_name’, $post->ID);
            foreach($fields as $field){
            if($field[‘acf_fc_layout’] == “first_layout_name”){
            $content .= ” ” . $field[‘flex_sub_field_1’];
            $content .= ” ” . $field[‘flex_sub_field_2’];
            } elseif ($field[‘acf_fc_layout’] == “second_layout_name”){
            $content .= ” ” . $field[‘flex_sub_field_4’];
            $content .= ” ” . $field[‘flex_sub_field_5’];
            return $content;

            “You can try to print_r() your flexible and repeater custom field to see the structure of the returned field ( something like print_r(get_field(‘repeater_field_name’, $post->ID)) ). ”

            So far it is working very well.

  3. Hello. I am new at this, so thank you in advance for your patience.

    I have a video website that stores video tags (Field1) in one custom table (Table1) which has it’s own ID (vID1), and video descriptions (Field2) in another table (Table2) which has it’s own ID (vID2). These videos are kept in tables that are not associated with the typical WP posts and pages. At this time, neither WP nor my theme is capable of making my current search functionality include my videos tags or descriptions. I hope that Relevanssi will be my solution!

    I have tried this:

    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, ‘field1’, true);
    $content .= ” ” . $custom_field;
    $custom_field = get_post_meta($post->ID, ‘field2’, true);
    $content .= ” ” . $custom_field;
    return $content;

    … and I have also tried replacing the ->ID’s with the vID1 and vID2. I am not having any luck. Also, I don’t know if there are other function.php files out there, but I inserted this into the functions.php file I found in the root of my theme folder (Did I mention I was new at this?)

    Is it possible for me to include the fields in the tables I am after? Or might other work need to be done to make Relevanssi aware of my other tables/fields? Thanks!

      1. Wow! Thanks for the quick response Mikko. I’m not certain how tell that. I believe they are. I can see the videos in the wp_posts table, but the tags and descriptions are housed in other tables. I am able to find words that are in the titles in my search (which are in wp_posts), but not tags or words descriptions. I am hoping I am just doing the add_filter incorrectly.


        1. Yes, looks like they are a custom post type. In that case it’s very much possible.

          This filter is not helping the search, though. You need ‘relevanssi_content_to_index’ to add content to posts as they are indexed.

          Also, you can’t use get_post_meta() when the tags or descriptions are not stored in post meta. Instead you need to use database queries to fetch the data from the separate database table.

          How this is done exactly goes beyond what I can do for you here, but if you buy a Relevanssi Premium license, this is something I can do for you without extra payment.

          1. That sounds great, Mikko. However, I am concerned about performance. I do not have a lot of posts/pages (under 500) and the delay is extensive (well over a minute). Here is what I have.

            State of the Index

            Documents in the index: 498
            Terms in the index: 3826
            Highest post ID indexed: 2554

            It may be because of my settings. I had set ‘custom fields to index’ to ‘all’ and I was getting my custom tags, again, after very long delays. I attempted to delete-and-rebuild my index but my State of the Index remained the same. Maybe the ‘all’ is still in operation? I wish the ‘visible’ and ‘all’ were check box items so I could be certain they were enabled/disabled.

            I am confident that your scripting will make the search much faster, but I wasn’t seeing a good response rate even before I entered ‘all’. Is there a way I can send you my settings to see if I messed thing up on my end?


          2. Relevanssi will not make search faster; the default WP search is certainly faster.

            You have a very small index, that should not be a problem. Is the delay in indexing or in searching? If it’s in the indexing, that’s not a problem at all. If it’s in the searching, then you have a problem and you may be running a quite slow server – in that case moving the search outside your server to a cloud is a good idea.

            I’ll be glad to help if you buy a support license, otherwise I’ll have to say no.

          3. I am currently on a shared server. I’ll have to find out what my dedicated RAM is. That will eventually change, but I do not know when. I don’t mind the (very) slow index as much as I do my user’s search experience.

            Can you give me an idea of a service you recommend that works well with Relevanssi? (I currently use Essentially, I am trying to figure out what my total annual cost will be.

            Even more important, based on both your experience and the size of the index I have, can you tell me how long you think a search would take in the environment/service-level you recommend?

            Thanks. Again, I’m new to all this.


          4. With that size, really anything decent should do. I’ve run a site three times as large in a regular shared webhotel environment without issues.

            Indexing may take minutes, and that’s usual, but searches shouldn’t take more than couple of seconds.

            If you want a lightning-fast search, you should look to other options. Swiftype and AddSearch provide really fast searches, for example.

      2. …and to follow up from my last message(below), if I were to go to Dashboard > Posts, my videos would not show up there. That’s why I wasn’t certain if they were actual posts. But again, they do have records in wp_posts just like my other normal posts – only without the video’s tags and descriptions.

    1. The second functions has potential. Have you checked what the get_field() returns? That’s the first thing I’d see – is the function returning the data you’re expecting it to return?

      1. Thanks. ACF support recommended that I modify my code a little bit and use the get_field_object() instead of the get_field() function. I tried to do that with this code (, but my searches still aren’t including image titles or captions. I think I was putting the cart before the horse by focusing on search excerpts earlier. First and foremost, I need the image title’s and captions that correspond to the image ID’s in a post’s gallery field to be indexed. Do I need to query the wpdb directly? I’m so confused.

        1. Nevermind! It works now! I needed to click “continue indexing” in Relevanssi Settings and now words in the title’s and captions of a post’s gallery are included in the search.

          This is the code I added to my functions.php file that works:

          add_filter(‘relevanssi_content_to_index’, ‘add_extra_content’, 10, 2);
          function add_extra_content($content, $post) {
          $images = get_field(‘gallery’, $post->ID);
          foreach( $images as $image ) {
          $content .= ” ” . $image[‘title’];
          $content .= ” ” . $image[‘caption’];
          return $content;

          1. Hi,

            I am trying to get Relevanssi to register my Exif data from the photos uploaded. However, after add in the code, it does not show up on Relevanssi’s list to be indexed. I tried to click “Continue to Index” but no luck either.

            My code is here:

          2. That filter doesn’t do anything for indexing. In addition to ‘relevanssi_excerpt_content’, use ‘relevanssi_content_to_index’ (see Stacey’s comment above).

            Also, you probably need to pass the post ID parameter to get_post_thumbnail_id().

          3. Hi Mikko,

            I changed to the below, but the result is the same:

            add_filter('relevanssi_content_to_index', 'add_extra_content', 10, 2);

            function add_extra_content($content, $post) {
            if ( ! is_singular( 'download' ) || ! has_post_thumbnail( get_the_ID() )) :
            $post_id = get_the_ID($post);
            $image_id = get_post_thumbnail_id($post_id);
            $image = wp_get_attachment_metadata( $image_id);
            $image_meta = $image['image_meta'];

            if ($image_meta) {
            foreach($image_meta as $image_meta) {

            $date_shoot = date('d-m-Y', $image_metadata['created_timestamp']);
            $content .= " " . $date_shoot;

            return $content;


          4. I would recommend debugging the code: see that it runs, see that it gets the correct data. That’s the way to solve this; I can’t help you from afar.

  4. Hi, thank you very much for your great plugin. I have one question. Is it possible to search also in the image captions? I have a website where one post = one photo and only text content is in image caption. Stacey in this discussion writes something simmilar, but his code doesn`t work for me. Can you please write me what can I do? Thank you very much!

    1. “Everything works automatically” – not really… For my search engine the most important thing is to show some excerpt of searched fields. ACF better search works but the query returns no content.

  5. Hi, I’ve just got some more grey hairs and got the code on this page working so Relevanssi will use custom fields in it’s excerpt. With this code and “custom search result snippets” turned on, it will highlight the search term.
    One thing to watch out for, the ‘ ‘ and ” ” changed when I originally copied the code from higher up in this page, ensure these are correct so the code shows in the correct colours in code view.
    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;

    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);
    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;

  6. This looks like it would be a perfect solution, but I think one line is missing, as $fields is not defined prior to the first “if($fields)…”.

    I tried just copying “$fields = get_field(‘page_content’, $post->ID);” that follows later, but it’s not yielding any results.

  7. Mikko is right, the function has placeholders for normal custom fields , repeaters and flexible fields.
    No need to delete or change anything, just edit the field names.
    (If you don’t have repeaters for eg just leave that in)
    If you have more fields just duplicate the line/section.
    Note that “page_content” is the name of my flexible field acf.
    I forgot to change this to a placeholder in the above code.

  8. Well, I just can’t get this to work in the latest Avada theme with Pods as a custom post type! Relevanssi does index the custom fields “More Fields” in post and search results are correct with Title of post displayed; but, I can’t get any of the custom fields to display their value in the search results.

    1. Hank, you’ve checked the “Use custom field content for building excerpts” option on the Excerpts tab in Relevanssi settings? Your theme is using the_excerpt() to print out the excerpts? Are you seeing Relevanssi-generated custom excerpts in the first place?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.