Skip to content

Instantly share code, notes, and snippets.

@romac
Last active December 21, 2015 14:09
Show Gist options
  • Save romac/6317489 to your computer and use it in GitHub Desktop.
Save romac/6317489 to your computer and use it in GitHub Desktop.
A Wordpress plugin that integrates Advanced Search By My Solr Server with WPML. This plugin adds the language code of the document about to be stored in Solr to it, and filter search results by the current language.

Solr + WPML

This plugin adds the language code of the document about to be stored in Solr to it, and filter search results by the current language.

Installation

  1. Add the following field to your schema.xml: <field name="lang" type="string" indexed="true" stored="true"/>

  2. Activate the plugin.

  3. Go to Advanced Solr Search by My Solr Server settings and delete all documents, then reload them all.

  4. That's it!

Copyright (c) 2013 kryzalid.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
<?php
/*
Plugin Name: Solr + WPML
Plugin URI:
Description: Integrate Advanced Search By My Solr Server with WPML.
This plugin hooks into advanced-search-by-my-solr-server
and adds the language code of the document about to be stored
in Solr to it, and filter search results by the current language.
Version: 1.0
Author: Kryzalid
Author URI: http://kryzalid.com
*/
add_filter('mss_build_document', 'sw_filter_build_document', 10, 3);
add_filter('mss_query', 'sw_filter_query', 10, 2);
add_filter('mss_autocomplete', 'sw_filter_autocomplete', 10, 2);
function sw_filter_build_document(Apache_Solr_Document $doc, WP_Post $post, array $options = array())
{
$lang = sw_get_post_language_code($post);
if($lang) {
$doc->setField('lang', $lang);
}
return $doc;
}
function sw_filter_query(array $args, array $options = array())
{
if(defined('ICL_LANGUAGE_CODE')) {
$args['params']['fq'] = '+lang:' . ICL_LANGUAGE_CODE;
}
return $args;
}
function sw_filter_autocomplete(array $args, array $options = array())
{
if(defined('ICL_LANGUAGE_CODE')) {
$args['params']['fq'] = '+lang:' . ICL_LANGUAGE_CODE;
}
return $args;
}
function sw_get_post_language_code(WP_Post $post)
{
global $wpdb;
$query = $wpdb->prepare(
'SELECT `language_code` FROM `wp_icl_translations` WHERE `element_id` = %d LIMIT 1',
$post->ID
);
$res = $wpdb->get_row($query);
if(!is_object($res)) {
return;
}
return $res->language_code;
}
@Kateriine
Copy link

Hello, great plugin, thanks! Small question: search results are displayed by language but I can't have facets by language (they are all displayed). Any idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment