Truncate Searchable Attribute in JS, HTML or PHP
-
hi there
i’m having trouble setting my taxonomy term as a variable and truncating it for use in autocomplete.php via any method. has anyone ever done this before ? i was able to output the taxonomy variable, just cant shorten it now
-
I guess I’m not quite sure what you’re referring to with truncating, outside of the definition of truncate. Are you trying to reduce the amount of words/characters displayed for the term? Perhaps to fit things into the space provided by autocomplete suggestion results?
Have you checked out the attributesToSnippet configuration yet? https://www.algolia.com/doc/api-reference/api-parameters/attributesToSnippet/
This will snippet down by words instead of characters, but it is largely out-of-box functionality that Algolia provides, and is configuration available on each index.
Otherwise, I’d need to see some of what you’ve tried so far to offer some more ideas.
on my site, https://holisticremedysearch.com/ if you search anything in the searchbox, the results returned bring back the whole thing, i wanted to truncate that. tried traditional javascript and php string parsing but it kept taking the {{data.overview}} from the other post you helped with, as literal strings.
- This reply was modified 4 months ago by holisticremedysearch.
Looking at your incoming algolia results, you don’t have the
overview
property set up to be part of the snippet’ed data, and you’re also only outputting the full version ofoverview
to the template.You’re going to need to do something like this within your site’s code, or alternatively you can configure things in your Algolia Dashboard if you’re not wanting to control index settings via code.
Code version
function my_posts_index_settings( array $settings ) {
// In case you want/need overview to be searchable.
$settings['searchableAttributes'][] = 'unordered(overview)';
// How many words to truncate the field after
$settings['attributesToSnippet'][] = 'overview:15';
return $settings;
}
add_filter( 'algolia_posts_index_settings', 'my_posts_index_settings' );This code snippet will make the
overview
attribute both searchable, and also snippeted to 15 words.You’d need to save this code in your site, and then also utilize the “Push settings” UI buttons available for all the intended indexes to use these settings. Lastly, since this is a configuration change, you’d also need to do a bulk re-index so that all of the indexed posts get their snippeted overview version created.
If you’re using just the Algolia UI to manage configuration settings, let us know and we can point to where to go to make the snippeting setting change. The re-indexing step would still be needed even in this case.
which php file do I edit ? Tried this in the UI and got stuck on some other documentation I read
The code version that I provided above would go in say your active theme’s
functions.php
file or similar. Nothing that you’re editing in our plugin’s files.i added that but it doesnt seem to do anything
function wds_algolia_custom_fields( array $attributes, WP_Post $post ) {
// Eligible post meta fields.
$fields = [
'overview'
];
// Loop over each field...
foreach ( $fields as $field ) {
// Standard WordPress Post Meta.
$data = get_post_meta( $post->ID, $field );
// Advanced Custom Fields.
// @see https://www.advancedcustomfields.com/resources/get_field/
$data = get_field( $field, $post->ID );
// Only index when a field has content.
if ( ! empty( $data ) ) {
$attributes[ $field ] = $data;
}
}
return $attributes;
}
add_filter( 'algolia_post_shared_attributes', 'wds_algolia_custom_fields', 10, 2 );
add_filter( 'algolia_searchable_post_shared_attributes', 'wds_algolia_custom_fields', 10, 2 );
function my_posts_index_settings( array $settings ) {
// In case you want/need overview to be searchable.
$settings['searchableAttributes'][] = 'unordered(overview)';
// How many words to truncate the field after
$settings['attributesToSnippet'][] = 'overview:15';
return $settings;
}
add_filter( 'algolia_posts_index_settings', 'my_posts_index_settings' );See this part of my reply from last week:
You’d need to save this code in your site, and then also utilize the “Push settings” UI buttons available for all the intended indexes to use these settings. Lastly, since this is a configuration change, you’d also need to do a bulk re-index so that all of the indexed posts get their snippeted overview version created.
i did the following with no change observed in rendered HTML
- update functions.php
- press push settings (https://holisticremedysearch.com/wp-admin/admin.php?page=algolia-search-page)
- re index all content (https://holisticremedysearch.com/wp-admin/admin.php?page=algolia-search-page)
Based on what I’m seeing, you need to use the UI from https://holisticremedysearch.com/wp-admin/admin.php?page=algolia since we’re dealing with Autocomplete. So it’d be the UI and re-index for at least the “posts” row on that page.
no UI change seems to work.
Instead of trying to futz around with code for settings at the moment, we can always figure that out later, let’s instead have you use the Algolia dashboard to check and confirm things.
For example, https://dashboard.algolia.com/apps/YOUR APP ID HERE/explorer/configuration/wp_posts_post/snippeting
Once you fill in your application ID with this url, it should take you to the configuration panel and the snippeting section specifically. You’ll want to use their UI to add an attribute, specifically “overview”, and then in the number input, fill in how many words you want to limit the field to, for the snippeted version of the data. For example, say 20 words total.
- This reply was modified 4 months ago by Michael Beckwith.
i did that a while back when you first mentioned, it is not honoring it
Hmm. I’m managing to get that to work with the searchable posts index, but strangely I’m not managing for the autocomplete
posts_post
table, even with Algolia’s UI. I’ve also tried pushing code versions of the snippetable attributes and it’s still only coming through withcontent
. I’m trying some other things in the meantime.neither work for me but removing one index entirely will help troubleshooting efforts. i wanted to do in JS or PHP but couldn’t get a variable declared from Algolia meta data.
- This reply was modified 3 months, 3 weeks ago by holisticremedysearch.
i wanted to do in JS or PHP but couldn’t get a variable declared from Algolia meta data.
Not quite sure what you mean here or what you may be trying in this space.
At least from what testing I’ve managed, it does appear to be potentially an issue with older Autocomplete. That’s a topic we’re still trying to best figure out how to handle, since newer Autocomplete has breaking changes that prevent easy integration into WordPress search fields.
Only remaining idea I have is making your own trimmed version(s).
function my_trim_overview( array $shared_attributes, \WP_Post $post ) {
// Fetch $overview_field here.
$overview_field = 'Some text';
// Trim to 20 words
$words = 20;
// Store the manual trimmed version for usage in the index.
$shared_attributes['overview'] = wp_trim_words( $overview_field, $words );
return $shared_attributes;
}
add_filter( 'algolia_searchable_post_shared_attributes', 'my_trim_overview', 10, 2 );
add_filter( 'algolia_post_shared_attributes', 'my_trim_overview', 10, 2 );
- You must be logged in to reply to this topic.