Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrii-kvlnko/967e216ea519490348845602820b0474 to your computer and use it in GitHub Desktop.
Save andrii-kvlnko/967e216ea519490348845602820b0474 to your computer and use it in GitHub Desktop.
ElasticPress - Polylang
<?php
/**
* Filter Elasticsearch posts by current language.
*/
add_filter( 'ep_formatted_args', function( $formatted_args, $args ) {
if ( function_exists( 'pll_current_language' ) ) {
$lang = pll_current_language();
if ( $lang !== false ) {
$term = get_term_by( 'slug', $lang, 'language' );
$formatted_args['post_filter']['bool']['must'][] = [
'term' => [
'terms.language.term_taxonomy_id' => $term->term_id
]
];
}
}
return $formatted_args;
}, 10, 2);
/**
* Add language taxonomy to Elasticsearch database.
*/
add_filter( 'ep_sync_taxonomies', function( $selected_taxonomies, $post ) {
if ( function_exists( 'pll_is_translated_post_type' ) ) {
if ( pll_is_translated_post_type( $post->post_type ) ) {
$selected_taxonomies[] = get_taxonomy( 'language' );
}
}
return $selected_taxonomies;
}, 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment