relevanssi_phrase_queries

apply_filters( 'relevanssi_phrase_queries', array $queries, string $phrase, string $status )

This filter hook modifies the MySQL queries for matching phrases. This is mostly for internal purposes to plug in Premium features.

Parameters

$queries
(array) An array of arrays containing MySQL queries for matching the phrases. Each array has two indices: query for the actual query and target for the column in the Relevanssi index table, doc or item.

$phrase
(string) The current phrase.

$status
(string) A list of post statuses to use in queries.

More information

When Relevanssi does phrase matching, the MySQL query gets extra conditions. Relevanssi will restrict the search to posts that have the phrase appear in them. This is done with the queries this filter sees.

In general, there’s very little need to touch these queries. This filter hook is mostly for internal purposes; Relevanssi Premium uses this to add couple of extra queries in order to make phrases match user profiles and taxonomy terms.

A possible use case would be a plugin that stores data related to the posts in a separate table. Let’s assume this plugin stores data about posts in a table wp_plugin_table, with the column post_id matching the post ID and the column extra_data containing the text that Relevanssi should handle. We’ve already used a relevanssi_content_to_index filter hook to add that extra content to Relevanssi index, and now we want Relevanssi phrase matching to work with this data.

add_filter( 'relevanssi_phrase_queries', 'rlv_extra_data_phrases', 10, 2 );
/**
 * Adds phrase matching to target phrases in extra_data.
 *
 * @param array  $queries The phrase queries.
 * @param string $phrase  The current phrase.
 *
 * @return array The phrase queries.
 */
function rlv_extra_data_phrases( array : $queries, string : $phrase ) : array {
  global $wpdb;
  $query = "(SELECT post_id
  FROM {$wpdb->prefix}plugin_table
  WHERE extra_data LIKE '%$phrase%')";
  $queries[] = array(
    'query'  => $query,
    'target' => 'doc',
  );
  return $queries;
}

The query provided by this function will then be wrapped in OR (relevanssi.doc IN to restrict the search to only the posts that contain the phrase in the extra_data column.