relevanssi_ok_to_log

apply_filters( 'relevanssi_ok_to_log', bool $ok, string $query, int $hits, string $user_agent, string $ip );

Controls whether a query is logged or not.

Parameters

$ok
(bool) If true, log the query, otherwise skip. Default: true.

$query
(string) The search terms.

$hits
(int) The number of results found with the query.

$user_agent
(string) The user agent string from $_SERVER['HTTP_USER_AGENT'].

$ip
(string) The user IP address from $_SERVER['REMOTE_ADDR'], if IP address logging is enabled.

More information

When Relevanssi updates the log (in relevanssi_update_log() function in lib/log.php), first, the user agent is compared against the bot block list (which can be filtered with relevanssi_bots_to_not_log). If the user agent is not on the bot list, then the current user is checked. If the user is on the list of users from relevanssi_omit_from_logs, the process stops.

The IP address is then fetched from the $_SERVER global if that is enabled, it passes through the relevanssi_remote_addr filter hook (which allows for anonymizing the IPs) and then comes this filter hook. If this filter hook doesn’t return false, the query is then logged.

For bot control, the relevanssi_bots_to_not_log is preferred. For other filtering, this filter hook is the way to go. For example to ignore all users from the 192.0.x.x IP range, you could do

add_filter( 'relevanssi_ok_to_log', 'rlv_block_192_0', 10, 5 );
function rlv_block_192_0( $ok, $query, $hits, $user_agent, $ip ) {
  if ( '192.0.' === substr( $ip, 0, 6 ) ) {
    $ok = false;
  }
  return $ok;
}

By default, Relevanssi logs each search result page load. If someone goes through ten pages of results for the same search term, the term will be logged ten times. If you don’t like that, you can use this filter hook to make Relevanssi only log the first page searches. Add this to your site:

add_filter( 'relevanssi_ok_to_log', function( $ok ) {
    global $wp_query;
    if ( $wp_query->is_paged ) {
        $ok = false;
    }
    return $ok;
} );