SearchWP

searchwp\query\logic\and\token_threshold

Since: 4.1.12

Table of Contents

Controls the threshold that SearchWP considers when deciding whether to perform an AND logic pass.

AND logic can be problematic resource-wise and in some cases result in long query times.

Because of SearchWP’s relevance algorithm, skipping AND logic because there are too many search terms will not degrade the quality of results as the most relevant results should still show up first. The benefit of AND logic is that there will be fewer results that contain all search terms, but skipping AND logic will likely not have a drastic effect on the results overall.

You have full control over this threshold, and you can customize the threshold at runtime to best meet your needs.

See also: searchwp\query\logic\and

Parameters

Type Parameter Default Since
Integer $threshold 5 4.1.12
Array $args
Key Type Value
tokens Array Tokens used for the search
query \SearchWP\Query Reference to the query
4.1.12

Examples

All hooks should be added to your custom SearchWP Customizations Plugin.

Disable AND logic token threshold

<?php
// Disable SearchWP AND logic token threshold, allowing AND logic for all searches.
// @link https://searchwp.com/documentation/hooks/searchwp-query-logic-and-token_threshold/
add_filter( 'searchwp\query\logic\and\token_threshold', '__return_false' );

How to use this code

Control AND logic token threshold

Customize the AND logic threshold based on the search tokens of the current search.

<?php
// Control SearchWP AND logic token threshold.
// @link https://searchwp.com/documentation/hooks/searchwp-query-logic-and-token_threshold/
add_filter( 'searchwp\query\logic\and\token_threshold', function( $threshold, $args ) {
// If the search contains 'coffee' allow up to 10 tokens for AND logic.
if ( in_array( 'coffee', $args['tokens'], true ) ) {
$threshold = 10;
}
// If the search contains 'soccer' disable AND logic token threshold.
if ( in_array( 'soccer', $args['tokens'], true ) ) {
$threshold = false;
}
return $threshold;
}, 20, 2 );

How to use this code