Hi,
This is a tough one, and is probably not possible. However maybe doing something like removing the “-” dash symbols programmatically could actually work to some extent.
Try this custom code functions.php in your theme/child theme directory. Before editing, please make sure to have a full site back-up just in case!
add_filter('asl_search_phrase_before_cleaning', 'asl_replace_characters', 10, 1);
function asl_replace_characters( $s ) {
$characters = "',._-?!"; // Type characters one after another
$replace_with = ' '; // Replace them with this (space by default)
if ( is_array($s) ) {
if ( isset($s['s']) && !$s['_ajax_search'] )
$s['s'] = str_replace(str_split($characters), $replace_with, $s['s']);
} else {
$s = str_replace(str_split($characters), $replace_with, $s);
}
return $s;
}
With this code enabled, searching “A-544-g4” will actually search “A 544 g4”. Now, if you set the keyword logic to OR, then it will match the original product “A-544”.
Best,
Ernest M.
Oh wow Ernest,
thank you so much for this hint …
i never thought about such a simple string operation 😉
i just changed a bit your code, especially to use REGEX wich can be much more accurate in string replacements 🙂
the new code:
add_filter('asl_search_phrase_before_cleaning', 'asl_replace_characters', 10, 1);
function asl_replace_characters( $s ) {
$characters = '/(?<!B)[.-]/';
$replace_with = ' ';
if ( is_array($s) ) {
if ( isset($s['s']) && !$s['_ajax_search'] )
$s['s'] = preg_replace($characters, $replace_with, $s['s']);
} else {
$s = preg_replace($characters, $replace_with, $s);
}
return $s;
}
I also added a negative lookahead because i do not want the script to search for a single character like “B”… this ended up in unqualified results.
thanks a lot
best robert
That looks really great, thank you for sharing 🙂
If you don’t mind, I will close this topic soon and mark it as resolved. Feel free to rate the plugin, it is greatly appreciated.
Best,
Ernest M.
Yes you can close it as resolved 🙂
thanks again and i will leave you a 5 star rating !
best robert