0

I am trying to create a custom search similar to the one on totaljobs.com but through a WordPress site. Essentially I would be able to search for both search terms and location simultaneously.

The way it's working now is that there is a search term field and then there is also a location search. They are two separate fields. For the search term field, I'm using the standard WordPress search and for the location search I am taking the jobs latitude and longitude and a radius setting and running them through the Google Maps API. As you can see, the search process is quite different on both searches.

I've added custom fields to the posts for latitude and longitude and am querying them in the database for my search. What I don't know is how the standard WordPress search works. Is it looking in the post_content and post_title field, etc.? I can't seem to find the server side code to investigate it because the search.php is more of a generic loop code.

I've found that $s is the global variable for search term but I can't find where that variable is being processed and am interested in how the database uses that variable because I think I'll need to build around the current WordPress search info.

I appreciate the help. Thanks!

1

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%')))
  • This is very in depth and the var_dump function is great (I'll definitely be using it for troubleshooting moving forward). I didn't realize creating your own search query was so simple. It should be documented somewhere easy to find! Thank you. – MxmastaMills Apr 14 '14 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.