WordPress.org

Support

Support » Plugins and Hacks » [Plugin: Search Unleashed] Displaying results by language

[Plugin: Search Unleashed] Displaying results by language

  • joaomorgado
    Member

    @joaomorgado

    Hi,

    I am using WordPress 3.0 with Silverio Theme and WPML tranlations.
    Since i am using a lot of Custom fields, i decided to take advantage of Search Unleashed but now i cant show search results by language.

    if i search on english page, it delivers all the results ignoring lang=en!

    Do you know how can i filter the search results by language?

    Thanks a lot

Viewing 2 replies - 1 through 2 (of 2 total)
  • joaomorgado
    Member

    @joaomorgado

    Any idea?
    My search results are showing all the posts: english and spanish. So i have duplicated entries…

    Is it possible to filter the language results by language?

    Hi Joaomorgado,

    I had a similar issue but using langswitch and I coded an easy workaround for the MYSQL fulltext search engine.

    Here are the things I added and their role:

    1. In search-unleashed/engines/mysql.php:
    In install_engine(), replace:
    $post = "CREATE TABLE'{$wpdb->prefix}search_post'('post_id'int(11) unsigned NOT NULL,";
    with
    $post = "CREATE TABLE'{$wpdb->prefix}search_post'('post_id'int(11) unsigned NOT NULL, post_lang CHAR(2) DEFAULT NULL,";

    This will create a new column in the wp_search_post table in the database that can hold the language and can then be used to retrieve only posts in a given language.

    2. In search-unleashed/models/spider.php
    In gather_for_post, just before returning $fields, you need to add code to add the correct language for each post in the database. This is unfortunately dependent on your ML plugin so you will have to figure out how to get a post language code (ex:en,es) from WPML. It should be quite easy.
    For my plugin (langswitch + a custom plugin I called langnav), it looks like this:
    $post_lang = langnav_get_post_lang($post_entry);

    Then, you have to insert the language in the fields so it will be inserted in the DB along with the post.
    The full code looks like this:

    if (function_exists('langnav_get_post_lang')){
                        $post_lang = langnav_get_post_lang($post_entry);
                        if($post_lang){
                            $fields['post_lang'] = array( 'data' => $post_lang, 'priority' => 1);
    }
    }

    3. In search-unleashed/engines/mysql.php
    In posts_request(), change the line:
    $sql .= " WHERE 1=1 ";

    With

    if (function_exists('langswitch_current_lang')){
    $sql .= " WHERE WP_SEARCH_POST.post_lang='".langswitch_current_lang()."'";
    }
    else{
    $sql .= " WHERE 1=1 ";
    }

    Note again that here langswitch_current_lang() returns the language we want the posts in. If you want to use something like lang=en, then it would look like this:

    if ($_GET['lang']){
    $sql .= " WHERE WP_SEARCH_POST.post_lang='".$_GET['lang']."'";
    }
    else{
    $sql .= " WHERE 1=1 ";
    }

    OR like this:

    if ($wp_query->get('lang')){
    $sql .= " WHERE WP_SEARCH_POST.post_lang='".$wp_query->get('lang')."'";
    }
    else{
    $sql .= " WHERE 1=1 ";
    }

    That’s it!
    I know it’s a bit problematic that it’s not a generic solution that works with any ML plugin but I think it should help you get on the right tracks.
    Note that on my setup it works perfectly!

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Plugin: Search Unleashed] Displaying results by language’ is closed to new replies.