Configuring Relevanssi for a better search in WordPress

A problem about WordPress is the search that isn’t as powerfull we want.

We research many different plugins and found Relevanssi one of the best here is how to configure Relevanssi for wordpress.

 

In a project we were working be needed a better way to search for posts of an specific type.  It is a big project that pretend to display an old magazine archive of 100.000 posts.

The problem with the WordPress search is that brings the first post that finds with the search criteria.

We thoght to use Google search services but the client wanted a nice advance search to filter by magazine, date and categories with a nice result page.

We search arrownd and we decide to use the plugin called Relevanssi

Relevanssi is a WordPress plugin that fixes the search. You get best results first, with highlights showing the exact part of the document that matched the query.

What the plugin does is to create a table of words per post and asign a frequency.  Also has a clever way to configure the wait you want to asign to the diferent areas of the post like title, content etc.

The suport it is fine if you are a developer but a bit obscure to find the documentation.

This is the first quick desing of what we needed to implement.

 

You can define in the Relevanssi admin panel what to be index and will show any custom post type you have in your wordpress but the tricky part it is to define the filters.

I have to say that it is done in a proper way allowing to hack the query with different hooks but the documentation of this hooks it is very sad…

You can find the list of Relevanssi Hooks at:

http://www.relevanssi.com/user-manual/filter-hooks/

For the wp taxonomies (categories) and dates we used: relevanssi_modify_wp_query

You just need to add the filter and the function in the function file of your theme.

This is a sample to get custom categories for the taxonomy ait-dir-item-category

add_filter('relevanssi_modify_wp_query', 'magazine_tax_query');
function magazine_tax_query($query) {
   $tax_query = array();
   if (!empty($query->query_vars['magazine'])) {
       $tax_query[] = array(
           'taxonomy' => 'ait-dir-item-category',
           'field' => 'id',
           'terms' => $query->query_vars['magazine']
       );
   }
   if (!empty($tax_query)) {
       $tax_query['relation'] = 'OR'; // you can also use 'OR' here
       $query->set('tax_query', $tax_query);
   }
   return $query;
}

You can use the php get to react to different search pages or filters

This sample shows how to filter by date (this for example is not documented)

if (isset($_GET['to']) or ! empty($_GET['from'])) {
    add_filter('relevanssi_modify_wp_query', 'articles_date_query');
}
function articles_date_query($query) {
    $date_query = array();
    if (empty($_GET['from'])) {
        $from = 0;
    } else {
        $from = $_GET['from'];
    }
    if (empty($_GET['to'])) {
        $to = 0;
    } else {
        $to = $_GET['to'];
    }
    if (($from != 0) and ( $to != 0)) {
        $date_query[] = array(
            'after' => $from,
            'before' => $to,
            'inclusive' => true
        );
    } elseif (($from != 0) and ( $to == 0)) {
        $date_query[] = array(
            'after' => $from,
            'inclusive' => true
        );
    } elseif (($from == 0) and ( $to != 0)) {
        $date_query[] = array(
            'before' => $to,
            'inclusive' => true
        );
    }
    $query->set('date_query', $date_query);
    $query->date_query = $date_query;
    return $query;
}

This other snippet was used to order the search result list:

if (isset($_GET['arrangeby'])) {
    if ($_GET['arrangeby'] != 0) {
        add_filter('relevanssi_modify_wp_query', 'rlv_asc_date');
    }
}
function rlv_asc_date($query) {
    $query->set('orderby', 'post_date');
    if ($_GET['arrangeby'] == 1) {
        $query->set('order', 'ASC');
    } else {
        $query->set('order', 'DESC');
    }
    return $query;
}

 

 

 

 

 

Tags
About the author
Eduardo Silva was born in Buenos Aires, Argentina, and has being living in London for the past 15 years. With a background in psychology he is a IT developer and the co-founder of open-ecommerce.org, a digital content social enterprise. His passion is digital story-telling and has created short films and documentaries to help people promote there ideas.