• Hi,

    I’m trying to use the relevanssi_excerpt_content filter to show the content of my ACF custom fields in the search results page. The code I got from the comments of this post http://www.relevanssi.com/knowledge-base/relevanssi-excerpt-content/ works (the content of the ACF fields is showed) but strangely is also shows me the filename of the template the page is using (ex: xxx-xxx-page.php) and a series of numbers that I suppose refers to the fieldname stored in the database (ex: 1440978520:1 field_5474bdaad120a 831 default 63 field_5474bd336dc79).

    This is my code:

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

    return $content;
    }

    I need this filter because I have many pages that have no text in the default content editor, only custom fields, and without it, the page search results shows nothing but the page title.

    How can I hide those elements and show only the content of the ACF custom field?

    I’m not that good in PHP so… any help appreciated.
    Many thanks!

    https://wordpress.org/plugins/relevanssi/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mikko Saari

    (@msaari)

    That function reads in all meta fields. Instead of getting all the fields and looping through them, you can use get_post_meta($post->ID, 'fieldname', true); to only get the fields you want to include. Just figure out the names of the fields you want.

    Thread Starter solosimar

    (@solosimar)

    Hi Mikko, thanks for your reply.
    The problem is I have a great number of ACF custom fields and didn’t want to write all of them by hand, having to update the list whenever I created a new custom field… That’s why I was using

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

    instead of
    get_post_meta($post->ID, 'fieldname', true);
    But with global $wpdb;, the search results are giving me not only the content of what is stored in the ACF custom fields but also the ACF custom field label itself (along with other unwanted stuff).
    I may have missed something or have some misconfiguration somewhere, but I cant’ figure out what 🙁

    Plugin Author Mikko Saari

    (@msaari)

    That code is not fetching field names, only the field content – but you may have invisible fields that have exciting content. You might actually want to try filtering with that – if you leave out invisible fields (ie. those with names starting with “_”), do you get cleaner results?

    Also, if you have fields that contain serialized arrays, those can also cause messy results. You may want to clean out those as well.

    Thread Starter solosimar

    (@solosimar)

    Mikko, I’ve just tried in a WP fresh install, and I’m getting the same results…

    With

    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 );
    }
    return $content;
    }

    the search results excerpts show me something like “text I wrote on mycustomfield1 field_55eedc91644aee text I wrote on mycustomfield2”.

    I get this no matter what I have in Relevanssi settings, under Indexing options > Custom fields to index (all / visible / mycustomfield1,mycustomfield2).

    With

    add_filter('relevanssi_excerpt_content', 'excerpt_function', 10, 3);
    function excerpt_function($content, $post, $query) {
    $fields = array('mycustomfield1','mycustomfield2');
    foreach($fields as $key => $field){
    $field_value = get_post_meta($post->ID, $field, TRUE);
    $content .= ' ' . $field_title[$key] . ' ' . ( is_array($field_value) ? implode(' ', $field_value) : $field_value );
    }
    return $content;
    }

    the search results excerpts show me something like “text I wrote on mycustomfield1 text I wrote on mycustomfield2”, which is I what I’m looking for… but without the need to write ALL my custom fields in $fields = array('mycustomfield1','mycustomfield2');.

    Do you have any other suggestions…? I’m using a child theme of the Cyberchimps Responsive theme (http://cyberchimps.com/responsive-theme/), if it helps.

    Plugin Author Mikko Saari

    (@msaari)

    What if you change this:

    $fields = $wpdb->get_col("SELECT DISTINCT(meta_key) FROM $wpdb->postmeta");

    to this:

    $fields = $wpdb->get_col("SELECT DISTINCT(meta_key) FROM $wpdb->postmeta WHERE post_id = $post->ID");

    Does that change anything?

    Thread Starter solosimar

    (@solosimar)

    It just changes the order in which the search results fields are showed. Instead of “text I wrote on mycustomfield1 field_55eedc91644aee text I wrote on mycustomfield2” I get something like “field_55eedc91644aee text I wrote on mycustomfield1 text I wrote on mycustomfield2”.
    Do you think this may have something to do with the WordPress version? I’m running 4.3 and both Advanced Custom Fields and Relevanssi plugins are compatible up to 4.2.4…

    Plugin Author Mikko Saari

    (@msaari)

    Ah, you’re using Advanced Custom Fields. I forgot that. That’s probably the reason. Can you check what is the name of the field that contains the “field_55eedc91644aee”? Perhaps you can solve this by excluding the problematic fields?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘relevanssi_excerpt_content showing fieldnames in search page results’ is closed to new replies.