Understanding WordPress Search

The default search is handled by WP_Query mostly by a method called parse_search(), which is triggered by the s parameter. You can search the source of WP_Query for is_search and piece together a few other bits and pieces.

Or you can just create a query…

$s = new WP_Query(array('s' => 'test'));

… dump the SQL…

var_dump($s->request);

… and read it.

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
WHERE 1=1
  AND (((wp_posts.post_title LIKE '%test%')
        OR (wp_posts.post_content LIKE '%test%')))
  AND (wp_posts.post_password = '')
  AND wp_posts.post_type IN ('post',
                             'page',
                             'attachment',
                             'book')
  AND (wp_posts.post_status="publish")
ORDER BY wp_posts.post_date DESC LIMIT 0,2

You should see that by default you get a LIKE query on the title and content. “Sentence” searches get broken apart like so:

AND (((wp_posts.post_title LIKE '%Hello%')
      OR (wp_posts.post_content LIKE '%Hello%'))
     AND ((wp_posts.post_title LIKE '%I%')
          OR (wp_posts.post_content LIKE '%I%'))
     AND ((wp_posts.post_title LIKE '%am%')
          OR (wp_posts.post_content LIKE '%am%'))
     AND ((wp_posts.post_title LIKE '%a%')
          OR (wp_posts.post_content LIKE '%a%'))
     AND ((wp_posts.post_title LIKE '%test%')
          OR (wp_posts.post_content LIKE '%test%')))