Skip to content

Instantly share code, notes, and snippets.

@msaari
Last active January 21, 2019 13:09
Show Gist options
  • Select an option

  • Save msaari/3097c904dc1c39ee787fd796c5550b4e to your computer and use it in GitHub Desktop.

Select an option

Save msaari/3097c904dc1c39ee787fd796c5550b4e to your computer and use it in GitHub Desktop.
Creative WPML filter
<?php
remove_filter( 'relevanssi_hits_filter', 'relevanssi_wpml_filter' );
add_filter( 'relevanssi_hits_filter', 'creative_wpml_filter' );
function creative_wpml_filter( $data ) {
$filtered_hits = array();
foreach ( $data[0] as $hit ) {
if ( is_integer( $hit ) ) {
// In case "fields" is set to "ids", fetch the post object we need.
$hit = get_post( $hit );
}
if ( isset( $hit->blog_id ) ) {
// This is a multisite search.
switch_to_blog( $hit->blog_id );
if ( function_exists( 'icl_object_id' ) ) {
// Reset the WPML cache when blog is switched, otherwise WPML
// will be confused.
global $wpml_post_translations;
$wpml_post_translations->reload();
}
}
global $sitepress;
$curr_language = apply_filters( 'wpml_current_language', null );
$post_language = apply_filters( 'wpml_post_language_details', null, $hit->ID );
if ( $post_language['language_code'] === $curr_language ) {
$filtered_hits[] = $hit;
}
if ( isset( $hit->blog_id ) ) {
restore_current_blog();
}
}
$query = '';
if ( isset( $data[1] ) ) {
$query = $data[1];
}
return array( $filtered_hits, $query );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment