This is the code I found on the algolia website, as the process changed I guess this solution is not working anymore.
It should add these data ‘wpml.locale’ in the post indexed and I have to filter the results afterwards.
<?php
// 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' );
add_filter( 'algolia_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 );
-
This reply was modified 5 years, 2 months ago by jeFFF-73.
-
This reply was modified 5 years, 2 months ago by jeFFF-73.