WordPress.org

Forums

Relevanssi - A Better Search
[resolved] include all custom fields in search excerpt (11 posts)

  1. dadra
    Member
    Posted 1 year ago #

    Relevanssi rocks.

    I'm building a site that uses custom fields quite heavily via the Advanced Custom Fields plugin. In relevanssi settings I have set "Custom fields to index" to 'all'. Now I want to show the custom fields content in my search result excerpts, and am following the instructions here:

    http://www.relevanssi.com/knowledge-base/add-custom-fields-search-excerpts

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

    However, this seems to require I enter each custom field name manually. My site uses so many custom fields that this would be very tedious. I'm also using many ACF Repeater fields and ACF Flexible Content fields which generate their own unique field names, so it's not realistic to enter them all.

    Is there a way to tweak the code above to display all custom field content in the search excerpts without having to enter them manually one by one?

    Thanks in advance!

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

  2. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    if you want to get all meta fields for a post, I think you need to query the database directly.

    SELECT meta_value FROM $wpdb->post_meta WHERE post_id = $post->id

    This will get you the meta field content from all the meta fields for one post.

  3. dadra
    Member
    Posted 1 year ago #

    Mikko, thanks so much for the response. I'm not sure how to integrate your suggestion into the original code above. Are you able to post the full code snippet containing your suggestion?

    Thanks again!

  4. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    add_filter( 'relevanssi_excerpt_content', 'custom_fields_to_excerpts', 10, 3 );
    function custom_fields_to_excerpts( $content, $post, $query ) {
        global $wpdb;
        $custom_fields = $wpdb->get_var( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $post->id" );
        if ( $custom_fields ) {
            $content .= " " . implode( " ", $custom_fields );
        }
        return $content;
    }

    Something like this.

  5. dadra
    Member
    Posted 1 year ago #

    Thank you Mikko. I tried the code you posted but my search results still show only the page/post titles. No content from the custom fields is included in the search result excerpt. I'm not strong in PHP so I don't know where to go with this. Any more suggestions? I'm certain that if we get this working right it will be a great benefit to many people, as so many wordpress sites are built using custom fields. Thanks!

  6. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    Try this: add a new custom field to a post, and in that custom field, insert a made-up word. Save the post and then try searching for that made-up word. Does it appear in the excerpt?

    Are you getting Relevanssi-generated excerpts in the first place?

    The problem may be the ACF fields, I'm not sure if they are actually stored in the wp_postmeta database.

  7. dadra
    Member
    Posted 1 year ago #

    Thanks Mikko. I tested the system per your suggestion but am not getting the made-up word in the excerpt.

    I am getting Relevanssi-generated excerpts in the first place, but only content entered in the default wp editor. Custom field content is not shown in the search excerpt.

    By following your instructions here...

    http://www.relevanssi.com/knowledge-base/add-custom-fields-search-excerpts

    ...I am able to show custom field content in the search excerpts, but it's not realistic for me to enter all the custom field names manually (with all the repeaters and flexible content fields I'm using there are hundreds of unique field names).

    I looked in my database and from what I can see all the ACF fields (including repeaters and flexible content) are stored in wp_postmeta > meta_key

    Another thing I've noticed, and this may be a completely separate issue, is that my excerpts are not showing the search term hits. "Create custom search result snippets" is ticked, and my search results template does use the_excerpt, but the excerpts only show the first lines from the page/post. I have successfully gotten custom search result snippets to work in other projects, and from what I can see all my settings and configurations are the same, so I'm not sure what's happening here.

    Thank you for the generous support!

  8. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    In the code I posted, I notice I've used $post->id, when it's actually $post->ID. Does correcting that help?

  9. dadra
    Member
    Posted 1 year ago #

    Thanks Mikko. Using $post->ID did not solve the problem. No change from what I can see. Any other suggestions?

    On the subject of custom search snippets not showing the search term hits, I'm seeing that search excerpts pulled from posts do indeed show the search term hits, while search excerpts pulled from pages do not show the search term hits. Any thoughts on that?

    Thanks for your time.

  10. Mikko Saari
    Member
    Plugin Author

    Posted 1 year ago #

    Are post and page excerpts shown with the same code? Is the page content any different from post content?

    As for solving the other problem, I would suggest closer debugging. Add some var_dump()'s in the code and see if it's being run, if it gets the custom field content as it should, if it adds it and so on. That way you can see where the problem lies.

  11. Lushon
    Member
    Posted 1 year ago #

    how come noones has helped me with my problem? i have opened a case but noone is helping. thanks

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Relevanssi - A Better Search
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic