Strange excerpt issues using ACF
-
1) We recently changed a post, but the old content is still showing in the Relevanssi search results. We rebuilt the index, but no change. Actually I can still find the old string in the DB, but it’s not in any of the ACF fields on that page, or even in the editor. Might it be in an old/unused ACF field? How/why would Relevanssi be pulling content that’s not visible on the page or even in the editor?
2) If we check “use custom field content for building excerpts” then we get relevant results from the NEW content on the page — which solves the problem for the page above. Unfortunately, for other pages it is causing ID#s, field names, and options that are NOT visible on the page to be displayed in the search results. On the Excerpts and highlights tab it says “Current custom field setting: all visible custom fields” but we’re still seeing non-visible ACF values in the search results. How do we keep non-visible ACF fields out of the search results?
Thank you!
The page I need help with: [log in to see the link]
-
Where exactly the old string is in the DB? Is it somewhere that Relevanssi sees?
With ACF, some metadata may also be in the visible custom fields; they may count as visible for Relevanssi but still be invisible in the editor.
With ACF, it would be best not to use “all visible custom fields” but instead list only the fields with the content you want to use. Relevanssi does ignore some ACF metadata fields but probably doesn’t catch them all. If you list only the fields you want to use, no unwanted data gets included in the index.
The option where we check “use custom field content for building excerpts” seems to be the most desirable, but we need to get the ACF field names and non-visible variables out of the excerpts. Example of the problematic results:
https://ben.edu/?s=alumniWe have dozens of Flexible Content fields and hundreds if not thousands of field groups within them, so listing the individual fields we want to include in the excerpts is not feasible. How can we ensure that only visible, on-page ACF content shows in the search results?
If listing individual fields is impossible, perhaps including or excluding fields based on a partial name would help. You can use the relevanssi_index_custom_fields filter hook to control which fields are indexed. The Knowledge Base has examples for including or excluding fields. If you have a good naming scheme for your fields and all your content fields end with
_text
,_content
or something like that, you can easily only include the fields with names like that.It takes some work to set up, but you only need to do it once. Relevanssi tries to exclude some ACF metadata automatically. Still, if you use lots of ACF fields, there’s also lots of unwanted content in the fields, and some manual cleanup is often required. Relevanssi can’t tell which fields have useful content and which don’t.
This is not an elegant or even practical solution. I will literally need to go through several hundred or possibly thousands of fields to determine what shows in search results. Why would it be normal functionality to include things like image IDs, and links — or radio/select options, that are intended for the back-end only? Wouldn’t it make more sense to index visible, on-page content only? (Maybe that means only text-based fields?) Or maybe you could integrate with ACF to add an “include in index” or “do not include in index” checkbox on each field. It would be much easier to go through each field group and select what you want or don’t want in the index, rather than trying to reverse engineer the search results and hunt for those fields in the back end, and then write manual exclusion code, as I’m apparently going to have to do. Just my two cents…
I’m also not having any luck with the “remove unwanted fields” code. Our pages use Flexible Content — would we need to add something more/different to exclude layouts or sub-fields within a Flexible Content group?
Thanks for your help.
- This reply was modified 2 years, 1 month ago by supernaut510.
Relevanssi is built to work with WordPress custom fields, and WordPress custom fields don’t have any metadata to them. They’re all just text fields.
The ACF fields have the types, but that type is pretty far removed from the actual field. The custom field has a metadata field, which points to a post slug and the post content has the field metadata in a format that is not MySQL compatible. I can’t just do a query that gives me a list of all text metadata fields.
That said, I haven’t thought of that before (my own ACF use is very basic), and I think there’s some potential. I suppose it would be possible to restrict Relevanssi to only specific field types somehow. But which field types? Text, text area, number, email, URL and range are pretty obvious. But what else? You mention radio buttons as something that’s only for the back end, but I can’t know that. Perhaps someone uses radio buttons for something that does make sense to index. So I’m not sure what’s the best way to approach this.
In any case, the field type is something you, as an end-user, can use to filter out the fields. It’s possible to create a filter function that makes Relevanssi only index text fields. Does that sound like a useful approach? If it does, I can help you build that.
Thank you. You’re right — radios and selects aren’t necessarily back-end only fields. None are necessarily one or the other by nature, I suppose. So the integration with ACF to specify whether to index each field would be great.
Barring that, I’ll have to manually include or exclude fields. As I mentioned, the code from https://www.relevanssi.com/user-manual/filter-hooks/relevanssi_index_custom_fields/ isn’t working for me. I wonder if it’s because I’m using flexible content? Would that pose a challenge for this method, since my fields are contained in layouts and sub-fields?
If it’s possible to include/exclude filed TYPES, that might also be very useful to me, as I almost always want to index a text field, but almost never want to index a radio button. If you can point me to any documentation that would suggest how to do that, I would be grateful.
Thanks again.
Which method you’re using from the page? Can I see the code? Flexible content isn’t a problem, but since the field names vary, you can’t really specify single field names. But the partial name method should work well if your fields have names that work with it; the method was created with flexible content cases in mind.
There’s no ready documentation for working with field types, as you’re the first one ever to raise the idea. I’ll come up with something. It would be a filter on the relevanssi_index_custom_fields hook.
But I’ll think of a good way to handle this. One option might be a filter hook in Relevanssi that has an array of true/false values for each field type in ACF, and you could set which types to include and which to exclude. That might be easy to do.
Looking at the ACF documentation, adding extra settings to the fields is possible. So Relevanssi could add an “exclude from search” checkbox to the fields. I’ll have to look into that as well.
It turns out ACF field-type filtering is very easy. Set Relevanssi to index “visible” custom fields, then add this:
add_filter( 'relevanssi_index_custom_fields', function( $fields ) { $indexed_fields = array(); foreach( $fields as $field ) { $object = get_field_object( $field ); if ( is_array( $object ) && isset( $object['type'] ) && 'text' === $object['type'] ) { $indexed_fields[] = $field; } } return $indexed_fields; } );
Then rebuild the index, and only
text
fields are included in the index.The next version of Relevanssi will add a “Exclude from Relevanssi index” setting to all ACF fields. You can disable individual fields from the Relevanssi index that way. Implementing that was very easy; ACF has a good API. Thanks for bringing this up. This is a solid improvement.
Thanks, I really appreciate your detailed & thoughtful help with this — I hope it will help other Relevanssi/ACF users as well.
Here’s the exclusion code I tried to implement, which isn’t working. It seems very simple, but…maybe you’ll see something I’ve done wrong?
//Remove ACF Flexible Content field values from Relevansi search results add_filter( 'relevanssi_index_custom_fields', 'rlv_skip_custom_fields' ); function rlv_skip_custom_fields( $custom_fields ) { $unwanted_fields = array( 'alignment', 'arrow1', 'arrow2', 'bg_color', 'bg_image', 'bullets', 'button_link', 'button_link1', 'button_link2', 'button_link3', 'button_link4', [many other fields omitted here] 'white_overlay' ); $custom_fields = array_diff( $custom_fields, $unwanted_fields ); return $custom_fields; }
The “Exclude from Relevanssi index” setting sounds like a lifesaver. Looking forward to that. Do you have a release date yet?
Your filter is trying to remove fields with those specific names, but those are not the actual names of the flexible content fields. With flexible content, you need to use the partial names method because the full name of the field in the database is
parentfield_1_alignment
,parentfield_2_alignment
and so on.add_filter( 'relevanssi_index_custom_fields', 'rlv_exclude_fields' ); function rlv_exclude_fields( $custom_fields ) { return array_filter( $custom_fields, function( $field ) { $exclude_suffixes = array( 'alignment', 'arrow1', 'arrow2', 'bg_color', 'bg_image', 'bullets', 'button_link', 'button_link1', 'button_link2', 'button_link3', 'button_link4', ); foreach ( $exclude_suffixes as $suffix ) { $suffix_length = strlen( $suffix_length ); if ( $suffix === substr( $field, -$suffix_length ) ) { return false; } } } ); }
I know better than to give specific release date estimates, but I guess November is vague enough.
I have tried implementing your code above, but now I’m getting NO results. There are fields named “title” and “text” and “text1” that are NOT in the array above, but they are not appearing in the excerpt. Might there be other plugin settings I need to tweak?
Sorry, there were a couple of errors in the code. This should work better.
add_filter( 'relevanssi_index_custom_fields', 'rlv_exclude_fields' ); function rlv_exclude_fields( $custom_fields ) { $filtered = array_filter( $custom_fields, function( $field ) { $exclude_suffixes = array( 'alignment', 'arrow1', 'arrow2', 'bg_color', 'bg_image', 'bullets', 'button_link', 'button_link1', 'button_link2', 'button_link3', 'button_link4', ); foreach ( $exclude_suffixes as $suffix ) { $suffix_length = strlen( $suffix ); if ( $suffix === substr( $field, -$suffix_length ) ) { return false; } } return true; } ); return $filtered; }
This seems to remove some, if not all, specified fields. (Caching may also be at play with the remaining items; Unsure…) This will get us through until the new feature rolls out. Thanks again for your help — you have been super helpful.
I wanted to bubble up this old post again — this discussion led to the “Exclude from Relevanssi Index” feature being added to ACF. I’m having an issue with that feature. The page template cycles through all of the different flexible content layouts that can be used on a given page (if the next flexible content layout used is X, get the corresponding template part). Strangely, the names of the layouts are being echoed in the search result excerpt.
I also have a question — if you exclude a field with subfields (like Repeater), do the subfields automatically get excluded? I notice they have the “exclude” option, but I’m not sure what’s being excluded if you choose to exclude the parent Repeater field.
Thank you!
- The topic ‘Strange excerpt issues using ACF’ is closed to new replies.