Custom Search Function – Ignore Diacritics
-
Hi,
I am new to PHP and WordPress world.
I am trying to customize my search box in Twenty Fourteen Theme so that it ignores Arabic diacritics/accents. This means if users search for “???????” it should be considered as “????”.
The default search query looks something like the below snippet:
SELECT wp_posts.ID FROM wp_posts WHERE 1=1 AND (((wp_posts.post_title LIKE '%keyword%') OR (wp_posts.post_content LIKE '%keyword%'))) AND wp_posts.post_type = 'post' AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC LIMIT 0,5
I need to skip accents in both fields (post_title and post_content) in addition to “keyword”. I tried to add the below snippet to functions.php in my child theme, but for some reason I am getting a fatal error.
function get_text_clear_of_diacritics($original_text){ return preg_replace("~[\x{064B}-\x{065B}]~u", "", $original_text); } function searchfilter($query) { if ($query->is_search) { $query->set('s', get_text_clear_of_diacritics($query['s'])); } return $query; } add_filter('pre_get_posts','searchfilter');
Your support would be appreciated.
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Custom Search Function – Ignore Diacritics’ is closed to new replies.