Skip to content

Instantly share code, notes, and snippets.

@algotrader-dotcom
Created January 23, 2019 03:25
Show Gist options
  • Save algotrader-dotcom/47d3862179d9b75985184491018a526d to your computer and use it in GitHub Desktop.
Save algotrader-dotcom/47d3862179d9b75985184491018a526d 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