Title: Search Functionality
Last modified: May 18, 2020

---

# Search Functionality

 *  Resolved [abuomama](https://wordpress.org/support/users/abuomama/)
 * (@abuomama)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/)
 * I’m working on a WordPress site that is using the native WP search functionality
   to search through the titles of posts in a specific post type. When I search,
   for example, ‘Mcdonalds’, no search results are shown. However, If I search for‘
   Mcdonald’s’ with an apostrophe, it will return the post titled “Mcdonald’s” –
   Is there any way to make the WordPress native search more “lax” on grammatical
   characters such as apostrophes, etc?

Viewing 10 replies - 1 through 10 (of 10 total)

 *  [a2hostingrj](https://wordpress.org/support/users/a2hostingrj/)
 * (@a2hostingrj)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12849894)
 * Ivory search plugin enhances the search form for those cases:
 * > [https://wordpress.org/plugins/add-search-to-menu/](https://wordpress.org/plugins/add-search-to-menu/)
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12851929)
 * [@abuomama](https://wordpress.org/support/users/abuomama/)
 * It’s called “fuzzy” matching. Try this :
    [https://en-ca.wordpress.org/plugins/relevanssi/](https://en-ca.wordpress.org/plugins/relevanssi/)
 *  Thread Starter [abuomama](https://wordpress.org/support/users/abuomama/)
 * (@abuomama)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12851957)
 * Thanks for the reply but I have wrote custom code using AJAX so I need custom
   functions.
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12852059)
 * [@abuomama](https://wordpress.org/support/users/abuomama/)
 * Then you could escape those characters :
    [https://www.php.net/manual/en/function.mysql-real-escape-string.php](https://www.php.net/manual/en/function.mysql-real-escape-string.php)
 *  Thread Starter [abuomama](https://wordpress.org/support/users/abuomama/)
 * (@abuomama)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12852742)
 * [@corrinarusso](https://wordpress.org/support/users/corrinarusso/) thanks for
   your time but let me know how could you escape here, see code –
 *     ```
       $query = new WP_Query(array('post_type'=>'services','posts_per_page'=>-1,'s'=>$searchVal));
         $data = array();
         if($query->have_posts()):
           while($query->have_posts()): $query->the_post();
             $location = get_field('service_location');
             $data[] = array(
                 'title' => get_the_title(),
                 'lat' => $location['lat'],
                 'lng' => $location['lng'],
                 'type' => 'service',
                 "path" => get_the_permalink()
             );
           endwhile;
         endif;
         $developments = $data;
         unset($data);
   
         echo json_encode($data);
       ```
   
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12853255)
 * [@abuomama](https://wordpress.org/support/users/abuomama/)
 * Untested, and I don’t know if there is already a native function to use this,
   but you could try to replace your query like this :
 * `$escapesearchVal = mysql_real_escape_string($searchVal);`
    but sanitize_text_field
   is probably safer and would already includes the escape : [https://developer.wordpress.org/reference/functions/sanitize_text_field/](https://developer.wordpress.org/reference/functions/sanitize_text_field/)
   [https://developer.wordpress.org/reference/hooks/sanitize_text_field/](https://developer.wordpress.org/reference/hooks/sanitize_text_field/)
   like this :
 *     ```
       $escapesearchVal = sanitize_text_field ( $searchVal ); 
       $query = new WP_Query(array('post_type'=>'services','posts_per_page'=>-1,'s'=>$escapesearchVal));
       ....
       ```
   
 *  [a2hostingrj](https://wordpress.org/support/users/a2hostingrj/)
 * (@a2hostingrj)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12853919)
 * Use you a filter to escape special characters in your custom ajax code:
 * > [https://wordpress.org/support/topic/search-with-special-characters-doesnt-work/](https://wordpress.org/support/topic/search-with-special-characters-doesnt-work/)
 *  Thread Starter [abuomama](https://wordpress.org/support/users/abuomama/)
 * (@abuomama)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12861901)
 * [@corrinarusso](https://wordpress.org/support/users/corrinarusso/) [@a2hostingrj](https://wordpress.org/support/users/a2hostingrj/)
 * I think you unable to understand my question completely so I recorded a video,
   watch – [https://youtu.be/5Y6ltSROOlg](https://youtu.be/5Y6ltSROOlg)
 *  [corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * (@corrinarusso)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12865394)
 * [@abuomama](https://wordpress.org/support/users/abuomama/)
 * Hmmm, tbh I am not a fan of the the solution described here :
    [https://wordpress.org/support/topic/search-with-special-characters-doesnt-work/](https://wordpress.org/support/topic/search-with-special-characters-doesnt-work/)
   It’s too explicit imo.
 * That said, did you try it out ? and does it work ?
    As I said earlier, I’m pretty
   sure the sanitize_text_field does the same thing. Did you try it ?
 * $escapesearchVal = sanitize_text_field ( $searchVal );
    $query = new WP_Query(
   array(‘post_type’=>’services’,’posts_per_page’=>-1,’s’=>$escapesearchVal)); ….
 *  Thread Starter [abuomama](https://wordpress.org/support/users/abuomama/)
 * (@abuomama)
 * [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12867649)
 * [@corrinarusso](https://wordpress.org/support/users/corrinarusso/)
 * I tried but nothing work, so I have created a field for keywords and then search
   worked as per I need

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Search Functionality’ is closed to new replies.

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 10 replies
 * 3 participants
 * Last reply from: [abuomama](https://wordpress.org/support/users/abuomama/)
 * Last activity: [6 years ago](https://wordpress.org/support/topic/search-functionality-20/#post-12867649)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
