Skip to main contentSkip to footer

Add more content to the index for Relevanssi Light

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.domain.com
 * Description: Overrides Relevanssi Light post data update.
 * Version: 1
 * Author: Mikko Saari
 * Author URI: https://www.relevanssi.com/
 * 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.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

WP Job Manager
Relevanssi doesn’t work with the WP Job Manager search. The solution is fortunately simple: you can just disable Relevanssi for…
Integrating Post Views Counter stats
It’s possible to integrate all kinds of external data to Relevanssi weights. Post Views Counter is an analytics plugin that collects stats…
FacetWP
Relevanssi is generally well compatible with FacetWP, which provides advanced filtering and faceting capabilities for the search. There are, however,…

Related Posts:

Comment Section:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed