Trying to use soundex() function in search so I can get the results whose value sounds like same.
I have created 6 posts.
- Hur
- Hur The Beautiful
- Hoor
- Hoor the Brand
- Hwr
- Hwr the beautiful
All above 6 posts contains (Hur, Hoor, Hwr) and sounds same "E600". How Can I overwrite the wordpress default search ? If I search for "Hur", it should return all 6 posts.
I have tried in Woocommerce as well, but it doesn't work. I have used multiple methods like "pre_get_posts" or "post_where" no of them worked properly.
add_filter('pre_get_posts', 'custom_post_search', 10, 2);
function custom_post_search($query)
{
global $wpdb;
if (is_search() || !is_admin()) {
$searchTerm = $query->get('s');
$searchTerm = "bad";
// $query = "";
// $query = "SELECT * FROM {$wpdb->posts} WHERE soundex(post_title) LIKE soundex($searchTerm) and post_type = 'post'";
// $query->set(soundex('post_name'), soundex($searchTerm));
// $query->set('post_name', soundex($searchTerm));
}
return $query;
}
I have done many changes in above code, But none of them worked. I have also used where filter.
add_filter('posts_where', 'attaching_custom_filter_where', 10, 2);
function attaching_custom_filter_where($where = "", \WP_Query $q)
{
global $wpdb;
$searchTerm = $q->get('s');
//$where = ""; // If I uncomment this to overwrite the query, it doesn't work.
$where .= ' OR soundex (' . $wpdb->posts . '.post_title) LIKE \'%' . soundex($searchTerm) . '%\'';
// Using OR will run default Wordpress Query along with our query, It shows other records too.
return $where;
}