Relevanssi Light

Relevanssi Light

Relevanssi Light is a lightweight variant of Relevanssi, which uses full-text indexes from your database server. The full-text index searches fast, with relevant results and easy configuration. On the downside, the search is not as customisable and versatile as regular Relevanssi.

When to use Relevanssi Light?

  • You don’t need customisation and don’t want to adjust settings.
  • You just want better results, fast.
  • You don’t need custom excerpts.

When to use Relevanssi Premium?

  • You need to index more content: terms, author names, user profiles, taxonomy term archives, attachment content and so on.
  • You want custom excerpts that highlight the search terms.
  • You want complete control over the index and the search results.

Instructions

The main customisation for Relevanssi Light is to add more content to the index. For custom fields, it’s easiest to use the relevanssi_light_custom_fields filter hook. Give the hook an array of custom field names, and the plugin will include the contents of those custom fields in the full-text index.

If you want to add more content, you may need to override the relevanssi_light_update_post_data() function. It’s pluggable so that you can override it from a must-use plugin. Here’s an example plugin that will include the post author’s name in the full-text index:

<?php
/**
 * Plugin Name: Relevanssi Light Override
 * Plugin URI: https://www.relevanssi.com/light/
 * Description: Overrides Relevanssi Light post data update.
 * Version: 1
 * Author: Mikko Saari
 * Author URI: https://www.mikkosaari.fi/
 * License: GPLv2 or later
 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
 */

if ( ! function_exists( 'relevanssi_light_update_post_data' ) ) {
  function relevanssi_light_update_post_data( $post_id ) {
    global $wpdb;

    // Fetch the author ID and use that to get the author name.
    $author_id = (int) $wpdb->get_var(
      $wpdb->prepare( "SELECT post_author FROM {$wpdb->posts} WHERE ID = %d ", $post_id )
    );
    $author_name = get_the_author_meta( 'display_name', $author_id );

    // Update the post to add the author name to the relevanssi_light_data column.
    $wpdb->update(
      $wpdb->posts,
      array( 'relevanssi_light_data' => $author_content ),
      array( 'ID' => $post_id ),
      array( '%s' ),
      array( '%d' )
    );
  }
}

Save this as a file in the /wp-content/mu-plugins/ folder.

Disabling Relevanssi Light for a specific post type

Here’s how you can disable Relevanssi Light for a particular post type. The example is shop_order from WooCommerce: you’ll need this if you use the legacy order storage. Otherwise, Relevanssi Light will stop the admin order search from working.

add_action('init', function() {
	add_filter( 'posts_search', function( $request, $query ) {
		if ( 'shop_order' === $query->query['post_type'] ) {
			remove_filter( 'posts_search', 'relevanssi_light_posts_search', 10 );
			remove_filter( 'posts_search_orderby', 'relevanssi_light_posts_search_orderby', 10 );
			remove_filter( 'posts_request', 'relevanssi_light_posts_request', 10 );
		}
		return $request;
	}, 1, 2 );
}, 11 );