Autocomplete and Instant Search – Bring in Custom Fields
-
does anyone know how to bring in Custom Fields into instant search or auto complete ?
i was able to see where to edit and how the title and content come in but need to switch existing content to a custom field in the auto complete and instant search.
-
In terms of getting the data indexed, which you’ll need to do in conjunction with any output in the results, you’ll want to check out some of our example code found at https://webdevstudios.com/2021/02/09/wp-search-with-algolia/#indexing-custom-fields
The example shows both regular
get_post_meta()
as well asget_field()
examples, in case you’re using say ACF Pro or similar. This could will also include into both autocomplete and instantsearch indexes, since they are going to be separate (unless you’re using “All Posts” for Autocomplete).Once you get those indexed, we can help with the display of them in the dropdown and instantsearch results, but I don’t know how far along you are overall just yet.
they are indexed, i am trying to show the data in the search results view for auto complete and instant. right now i am only showing the title, because i removed the excerpt since i wanted to show structured ACF data in the search results only.
You’ll want to edit that same spot and start displaying the indexed data instead.
Instead of having this:
<# if ( data._snippetResult['content'] ) { #> <span class="suggestion-post-content">
data._snippetResult['content'].value
</span> <# } #>I’m pretending that one of the custom fields indexed is
movie_excerpt
. You’d do something like this:<# if ( data._snippetResult['content'] ) { #> <span class="movie-excerpt">
data.movie_excerpt
</span> <# } #>And that would output the text in that indexed property.
sorry for the delay, i guess i am not following, can you confirm the steps ?
step 1 – index ACF custom fields via functions.php
Edit functions.php to add code block, no customization needed here.
>>> i have done this
step 2 – edit the plugin theme file for autocomplete.php
i tried this code, where post_title was the original thing that always worked and i added my custom field called “overview” but it doesn’t work.
data._highlightResult.post_title.value
data._highlightResult.overview.value
data._snippetResult['content'].value
data._snippetResult['overview'].value
not sure where i am going wrong here, i can repeat the post_title value many times in HTML output but custom fields don’t seem to be index or able to output to HTML.
When i look inside my Algolia index of items, i don’t see any attribute called “overview” either.
i tried adding the field “overview” in the functions.php snippet mentioned but that didn’t work either.
- This reply was modified 5 months, 3 weeks ago by holisticremedysearch.
- This reply was modified 5 months, 3 weeks ago by holisticremedysearch.
Do you have a live link that we could look at to see the incoming results and the data included in the hits?
Also since it would help, any code snippets of the code you’re adding for the indexing aspect of the custom fields so that we could see how you’re attempting that.
Regarding the custom field output thus far for what you’ve tried, they aren’t automatically going to be in the snippets/highlight result spot, unless you marked them as attributes for snippets or attributes for highlights.
the site is
https://holisticremedysearch.com/
not sure indexing part is working at all, maybe that is the first step ? Not sure how to inspect for data passed into the front end client via the console.
Re requesting this part:
Also since it would help, any code snippets of the code you’re adding for the indexing aspect of the custom fields so that we could see how you’re attempting that.
In regards to what I can see with the Autocomplete, I’m seeing the following attributes as highlight-able based on returned results:
post_title
,taxonomies
,content
. Attributes that are snippet-able would be justcontent
. Then for a term results, just these two are highlight-able:name
,description
sorry what is the difference between highlight-able & snippet-able ?
below i have
- ACF taxonomy – infections
- ACF custom field – overview (text i want to output to the auto complete and results)
- ACF taxonomy – medicine system
- This reply was modified 5 months ago by holisticremedysearch.
- This reply was modified 5 months ago by holisticremedysearch.
- This reply was modified 5 months ago by holisticremedysearch.
The attributes in the “highlight” section of a returned result is more or less the string that was queried for, and in this spot it has some html to use for highlighting. For example a background color to show.
For example:
Some <em>phrase</em> shown
when i theoretically searched “phrase”.The snippet section is more or less just showing part of an attribute in a result
https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/
https://www.algolia.com/doc/api-reference/api-parameters/attributesToHighlight/
ok, so which wordpress file do i edit and add this PHP to reference my taxonomies as a snippet attribute or highlight attribute ?
You’re going to want to make use of the available filters to modify these settings if you’re wanting to do so from directly in your own site’s theme files and whatnot. Alternatively you can also edit these settings in your Algolia dashboard. If you’re editing there directly, you’ll want to avoid clicking any of the “Push Settings” buttons because that’ll overwrite your settings with the defaults that the plugin has wired up for initial setup.
I guess my question, as it’s been a little while, is which are you wanting to handle? highlighting or snippeting? As a reminder, any of the attributes that are in the
searchableAttributes
part of your index’s settings should be highlightable unless you’ve specified only certain attributes. So perhaps review the settings shown in the screenshot below, which will be under your “Configuration” tab for the index in question.- This reply was modified 4 months, 3 weeks ago by Michael Beckwith.
- This reply was modified 4 months, 3 weeks ago by Michael Beckwith.
i see taxonomies but i don’t see my custom field called “overview”.
examples of taxonomies indexed, i want overview to show up as a span under the title
- This reply was modified 4 months, 3 weeks ago by holisticremedysearch.
- This reply was modified 4 months, 3 weeks ago by holisticremedysearch.
- This reply was modified 4 months, 3 weeks ago by holisticremedysearch.
- This reply was modified 4 months, 3 weeks ago by holisticremedysearch.
Sorry for the delay over a long holiday weekend in the US.
As long as the attribute is considered searchable, you should be able to find it when you click “Add a searchable attribute” under that list of post_title, taxonomies, and content.
ok, i added that.
Step 1: add to make it a searchable attributeStep 2: Edit PHP to show in HTML (for both auto complete or instant search)
Step 3: Observe HTML Output (https://holisticremedysearch.com/?s=dhea)… no data output on screen. tried snippet and high result
thanks so far, i feel like we are very close now, making more sense.
i got this to work for instantsearch.php and auto complete, thanks so much all this help!
<p>{{ data.overview }}</p>
- This reply was modified 4 months, 2 weeks ago by holisticremedysearch.
- You must be logged in to reply to this topic.