Terms in Autocomplete
-
I have enabled and indexed a term for autocomplete along with my post types but the term does not show in the autocomplete.
The term index in is Algolia, it’s enabled in the Autocomplete settings, it’s been indexed.
Anything I’m missing to show term results in the autocomplete dropdown?
-
Do you have a link where I could try this out myself and see if anything stands out? Last I knew, if it was checked as enabled in settings, it should automatically be getting a section rendered in the generated dropdown in its own little section.
To be certain, you also have tags that have been assigned to posts, correct? The resulting links are to the tag archive permalink, but it doesn’t return a tag with no posts yet.
It’s a custom taxonomy and yes the terms are all assigned to posts in a custom post type.
I don’t have a current staging link updated yet but when I do I’d have to send it privately if possible.Just as a quick example, I have a post type of “movie” and a taxonomy of “genre”, and at least 1 movie post tagged with “horror”. Click the image as needed for a bigger view.
In the screenshot you should see how a tag result shows for my
wdsa_terms_genreindex query from my browser dev console, when watching for XHR requests. I have the “Response” tab chosen to show the returned data from Algolia/Autocomplete.Hopefully you’re seeing similar things with your XHR requests and the intended
$prefix_terms_$taxonomyslugindex that would be generated for you.-
This reply was modified 1 year, 2 months ago by
Michael Beckwith.
Ya now I see its making the request for the terms but not returning any hits for it.
I’m overriding autocomplete.php in my theme folder and if I remove it, the terms show up. Looks like I must have removed something before, I’ll compare my file vs the plugin template file.
ThanksOh, I’m using WPML, when I add the filter, it no longer matches the terms:
https://github.com/WebDevStudios/wp-search-with-algolia/wiki/WPMLsource: algoliaHitsSource( client.initIndex( config[ 'index_name' ] ), {
hitsPerPage: config['max_suggestions'],
attributesToSnippet: [
'content:10'
],
highlightPreTag: '__ais-highlight__',
highlightPostTag: '__/ais-highlight__',
filters: 'wpml.locale:"' + current_locale + '"', // This is the added line.
}),This is to other filter I have for WPML:
// Add the locale of every post to every record of every post type indexed.
function add_locales_to_records(array $attrs, WP_Post $post)
{
// Here we make sure we push the post's language data to Algolia.
$attrs['wpml'] = apply_filters('wpml_post_language_details', null, $post->ID);
return $attrs;
}
add_filter('algolia_post_shared_attributes', 'add_locales_to_records', 10, 2);
add_filter('algolia_searchable_post_shared_attributes', 'add_locales_to_records', 10, 2);
// Register the locale attribute as an Algolia facet which will allow us to filter on the current displayed locale.
function add_locale_to_facets(array $settings)
{
$settings['attributesForFaceting'][] = 'wpml.locale';
return $settings;
}
add_filter('algolia_searchable_posts_index_settings', 'add_locale_to_facets');
// Expose the current locale of the displayed page in JavaScript.
function enqueue_locale()
{
wp_add_inline_script('algolia-search', sprintf('var current_locale = "%s";', get_locale()), 'before');
}
add_action('wp_enqueue_scripts', 'enqueue_locale', 99);/**
* Change searchable attributes and facets for 'pillar-achievement' index
*
* @param array $settings
* @return array
*/
function my_pillar_achievement_index_settings(array $settings)
{
$settings['searchableAttributes'] = [
'unordered(name)',
];
// Configure facets for filtering
$settings['attributesForFaceting'] = [
'wpml.locale'
];
return $settings;
}
add_filter('algolia_terms_pillar-achievement_index_settings', 'my_pillar_achievement_index_settings');I pushed this setting to the term index and “Attributes for Faceting” shows:
Unknown Attribute:
We couldn't find the attribute wpml.locale in a small sample of your records; there might be a typo.The algolia index for the term doesn’t have the wpml locale data like the posts do from this filter.
Is there any way to push the wpml details for terms? I don’t see a filter listed in the plugin docs.function add_locales_to_records(array $attrs, WP_Post $post)
{
// Here we make sure we push the post's language data to Algolia.
$attrs['wpml'] = apply_filters('wpml_post_language_details', null, $post->ID);
return $attrs;
}
add_filter('algolia_post_shared_attributes', 'add_locales_to_records', 10, 2);
add_filter('algolia_searchable_post_shared_attributes', 'add_locales_to_records', 10, 2);-
This reply was modified 1 year, 2 months ago by
jinsley8.
Some thoughts regarding the overall process here.
Any time you’re changing index settings, you’ll want to push those settings, as you’re generally doing already.
Any time you’re changing what parts of the content get indexed, you need to do a bulk re-index to push the new attributes to the index. It’s not going to automatically know about the
wpmlproperty to create, or what content to store in it. Doing the bulk re-index will update your records.For updating the data that gets indexed when indexing term data, you’ll want to use the filters found in https://github.com/WebDevStudios/wp-search-with-algolia/blob/2.8.2/includes/indices/class-algolia-terms-index.php#L91-L110. this will replace the
algolia_post_shared_attributesandalgolia_searchable_post_shared_attributesfilter names. I believe you’d want to add a filter callback foralgolia_term_pillar-achievement_recordbased on code examples above.I got it to add the wpml.locale data, but it is only indexing the English terms and not the 2nd French versions.
I may remove this term index as it’s not detrimental to the success of the project.function add_term_locales_to_records(array $record, $term)
{
$current_lang = apply_filters('wpml_current_language', null);
$language_info = apply_filters('wpml_active_languages', null, array(
'skip_missing' => 0,
'orderby' => 'code'
));
$record['wpml'] = array(
'language_code' => $current_lang,
'locale' => isset($language_info[$current_lang]) ? $language_info[$current_lang]['default_locale'] : get_locale(),
'text_direction' => false,
'display_name' => isset($language_info[$current_lang]) ? $language_info[$current_lang]['translated_name'] : 'English',
'native_name' => isset($language_info[$current_lang]) ? $language_info[$current_lang]['native_name'] : 'English',
'different_language' => false
);
return $record;
}
add_filter('algolia_term_record', 'add_term_locales_to_records', 10, 2);
add_filter('algolia_term_pillar-achievement_record', 'add_term_locales_to_records', 10, 2);Honestly unsure on the rest of that, as I don’t have a lot of hands on experience with Algolia + WPML especially with an already being developed website vs me trying little things.
While we definitely do offer the ability to index taxonomies, I do wonder how often that proves useful for most. Always open to hearing usecases though.
-
This reply was modified 1 year, 2 months ago by
The topic ‘Terms in Autocomplete’ is closed to new replies.
