how to add synomyms?
-
i already create a code that working with regular search wordpress, but how to implementing to relevanssi light? i use regular relevanssi but it very slow
$synonyms = array(
'car' => array('automobile', 'vehicle', 'auto', 'motor'),
'bread' => array('food', 'feast', 'loaf', 'bun')
); // Modify the WordPress search query to include synonyms
function add_synonyms_to_search($query) {
// Check if it is a search query and not in admin area
if ($query->is_search && !is_admin()) {
// Get the original search term
$search_term = $query->get('s');
// Split the search term into words
$words = explode(' ', $search_term);
// Loop through each word
foreach ($words as $word) {
// Check if the word has any synonyms in the array
if (array_key_exists($word, $synonyms)) {
// Loop through each synonym
foreach ($synonyms[$word] as $synonym) {
// Add the synonym to the query with an OR operator
$query->set('s', $query->get('s') . ' OR ' . $synonym);
}
}
}
}
// Return the modified query
return $query;
}
// Hook the function to the pre_get_posts action
add_action('pre_get_posts', 'add_synonyms_to_search');how to implementing it to relevanssi light?
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘how to add synomyms?’ is closed to new replies.