wpfts_search_terms (Filter)
The wpfts_search_terms
filter in WP Fast Total Search allows developers to modify user-entered search terms before they are used for searching. This enables modifying, adding, or removing terms, providing flexible search logic customization.
When to Use
This filter can be helpful in the following situations:
- Preprocessing search queries: e.g., removing punctuation, converting to lowercase, correcting typos.
- Expanding search queries: e.g., adding synonyms or related terms to the original query.
- Excluding specific terms from the search.
- Implementing “smart” search, which automatically corrects user queries.
Arguments
$terms
(array): An array of search terms. Each array element represents a single term (word or phrase).$wpq
(object): TheWP_Query
object containing the parameters of the current query. This object can be used to access other query parameters, such as post type, post status, etc., allowing for more complex filtering logic.
Return Value
$terms
(array): The modified array of search terms.
Example
/**
* Removes punctuation from search terms and converts them to lowercase.
*/
add_filter('wpfts_search_terms', 'my_wpfts_search_terms_filter', 10, 2);
function my_wpfts_search_terms_filter($terms, $wpq) {
$processed_terms = array();
foreach ($terms as $term) {
// Remove punctuation.
$term = preg_replace('/[^a-zA-Z0-9\s]/', '', $term);
// Convert to lowercase.
$term = strtolower($term);
if (!empty($term)) { // prevent empty values
$processed_terms[] = $term;
}
}
return $processed_terms;
}
Important Notes
- Changes made to the
$terms
array directly affect search results. Exercise caution when modifying terms. - The
wpfts_search_terms
filter is called before the search is executed, so it can be used to optimize the query and improve performance.
The wpfts_search_terms
filter provides developers with a powerful tool for fine-tuning search logic in WP Fast Total Search.