Support

Account

Home Forums General Issues Making customfields searchable?

Solved

Making customfields searchable?

    • Bondra

    • February 17, 2014 at 6:28 pm

    When making a search I won’t get any search results from any content within my custom fields. After some research I tried different plugins (Relevanssi among others) that made it possible to select what custom fields to search in … The problem though, the search result won’t print any excerpt from the fields just a link with the name of the page where content from the custom field was found.

    How can I get the excerpt from chosen custom fields? I need to get excerpts printed from fields used with the the_field but also fields used in the repeater add-on.

    • Elliot

    • February 18, 2014 at 8:57 am

    Hi @bondra

    I’m sure that plugins such as relevanssi offer a filter to modify the output HTML for each result. You can use this to load in the ACF data.

    Thanks
    E

    • Ash

    • March 4, 2014 at 8:23 pm

    Hi,

    I’ve been having a similar issue for a while now. I use the flexible content field to allow my clients to create columns within WordPress and other elements they may need.

    I’ve been working on a solution for a while (I’m no PHP developer, so forgive me here), I’ve gotten close thanks to stumbling onto this post from the old support site http://goo.gl/flryRN

    I’ve had to amended it a little for my needs & it isn’t perfect at all, but maybe we can get to a better solution together?

    You will need to use Relevanssi, with this code added to your functions.php file…

    // Get Relevanssi to display excerpts from your custom fields
    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; }

    This works, but (and there is a but) it seems to display post revisions, so if you’re like me and use Latin as place holders for dummy content during the build process, it shows up in the results. Also, it seems to show the custom field IDs at times too, depending where your search term shows up in the order of the ACF field content.

    I’ve not figured out a solution to the revisions yet, but to remove the IDs I’ve tweaked the code for this…

    // Get Relevanssi to display excerpts from your custom fields
    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 ); }
    
    // Remove random terms from showing in the search. These are related to the names of the ACF field names
    $wordlist = array('acf_id_1', 'acf_id_2', 'acf_id_3', 'acf_id_4');
    foreach ($wordlist as &$word) {
        $word = '/\b' . preg_quote($word, '/') . '\b/';
    }
    
    $content = preg_replace($wordlist, '', $content);
    
    // The excerpt ready with bits removed from it
    return $content; }

    Again, not ideal, but works of sorts.

  • I have installed Relevanssi but I am still not getting any results for pages that use my Flexible layout that I have templated.

    eg: add a text area as a flexible content item, displays fine, but when searching for words in the text area, nothing shows up.

    Is there a setting in Relevanssi that I am missing?
    It is the first time I have used the Relevanssi plugin.

    Thanks, Sam

  • Never mind… missed the setting for Custom Indexing. Set it to ‘visible’ and is working a treat.

    Thanks for the code Cloud9 🙂

    • Ash

    • March 12, 2014 at 8:21 pm

    @frysteen Thanks for the thanks 🙂

  • @cloud9 … sorry to be a pest, but I was wondering how I can limit the search of my flexible content content to only search certain flexible content items?

    eg: If my flexible content items include a text box, a gallery, a banner layout, etc… how can I limit the searching to ONLY look at the text in the Text Box modules?

    As I have relevanssi set to search all “visible”, it is seeing every custom field I have on the page, most of which should not be searchable.

    Hop this makes sense. It’s all a bit above my coding level.

    Appreciate your help mate, Sam.

    • Ash

    • March 13, 2014 at 8:02 pm

    @frysteen … Sorry, I’m not totally sure. I’m not a backend developer, more front-end. Perhaps using this post as a starting point can point you in the right direction.
    http://goo.gl/flryRN Jan, starts off by building an array of items to search. Maybe you could do an if statement that singles out the fields you want?

    If you get to a solution I would be keen to know what it is.

    • Ash

    • February 10, 2015 at 4:18 am

    @frysteen Did you fix your issue?

  • @cloud9 did you ever manage to clean up this solution to remove all the unwanted stuff? I find it is partly working but an awful lot of junk is appearing in the snippets alongside the useful stuff. eg

    …4 1449577239:1 page-da-default.php The International Ecotourism Society defines ecotourism as “responsible travel to natural areas which conserves the environment and improves the welfare of the local people.” field_5639fb21efdb8 darkblue field_563a2064a37b9 none field_563a2099a37ba three_column three_column three_column field_5639fa6d148b5 0 field_563a28c817505

    Like you I am out of my depth trying to fix this on my own – but I think you know more than I do!

    • Ash

    • December 15, 2015 at 8:21 pm

    @juliancoates34
    Hi, I had looked time and time again at this, but I came up with a solution (with the help of others) here http://support.advancedcustomfields.com/forums/topic/yoast-seo-acf/

    However, I’m sorry to say that it seems that Yoast has changed his method of doing things and it seems to be not working at the moment again 🙁

  • If someone is looking for a solution to this problem is to recommend the plugin:
    https://wordpress.org/plugins/acf-better-search/

    This plugin adds to default WordPress search engine the ability to search by content from selected fields of Advanced Custom Fields plugin.

    Everything works automatically, no need to add any additional code.

    • Ash

    • March 13, 2017 at 8:16 pm

    ❤ Favorited

    • agdjk

    • September 5, 2017 at 2:56 am

    Does anyone know how to create excerpts with the ACF Better Search plugin? I’ve tried the relevanssi code and editing it a little but I can’t get it to work…

    • ukguy

    • October 29, 2017 at 3:30 pm

    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;
    }
Viewing 16 posts - 1 through 16 (of 16 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.