analysis on wordpress search form code

WordPress sites often have a search box on the navbar or sidebar. Type some words in it and press enter, you will get some posts displayed on the search results page. The search results are somewhat amazing because the search terms even do not occur on the post titles. We will analysize the wordpress search form code so that you can understand how the search results are obtained and how they are ordered on the search results page. Understanding the wordpress search box code can also help you create a custom search form to implement more  functions.

In the frontend, the search box code is an html form  with an input box for you to type the search words, and a submit button(although the button is usually hidden). You can modify the wordpress search widget source code to customize the appearance of the search box. You can search multiple words  (separated by white space or comma) in the input box such as “keyword1 keyword2, keyword3″. After press the enter button, the search words will be transported back to the homepage via the _GET[‘s’] variable, and finally go into the query vars. A function wp( ) in the file wp-include\functions.php is called to query the database to retrieve posts related to the search words.    Specifically, wp() calls the class WP’s member function main() , which calls query_posts(), the latter calls $wp_the_query->query($this->query_vars) which calls get_posts() to get the related posts. The main work of get_posts() is to construct a sql query string to query the database to get the posts. Our search words will be used to form the WHERE clause by the  parse_search( $q ) function to implement the search function. parse_search first eliminates the so called stop words from our search words. The wordpress search excluded words are:

about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www

The function will eventually get the following search sql query.

"((wp_posts.post_title LIKE '%keyword1%') OR (wp_posts.post_content LIKE '%keyword1%')) AND ((wp_posts.post_title LIKE '%keyword2%') OR (wp_posts.post_content LIKE '%keyword2%')) AND ((wp_posts.post_title LIKE '%keyword3%') OR (wp_posts.post_content LIKE '%keyword3%'))"

From the clause above, we can see that the relationship between the search words is AND, i.e., the result posts must include all search words in either post title OR post content(that explains why some of our keywords do not appear in post title. ) If you want WordPress to search whole words only, you should change the search logic here.

get_post also calls parse_search_order to form the order by clause, which determines how the result posts are ordered on the search results page. In our case, we got the following order by clause:

"(CASE WHEN wp_posts.post_title LIKE '%keyword1 keyword2, keyword3%' THEN 1 WHEN wp_posts.post_title LIKE '%keyword1%' AND wp_posts.post_title LIKE '%keyword2%' AND wp_posts.post_title LIKE '%keyword3%' THEN 2 WHEN wp_posts.post_title LIKE '%keyword1%' OR wp_posts.post_title LIKE '%keyword2%' OR wp_posts.post_title LIKE '%keyword3%' THEN 3 WHEN wp_posts.post_content LIKE '%keyword1 keyword2, keyword3%' THEN 4 ELSE 5 END)"

From the clause above, we know that the posts whose title contain the exact string we input in the search box will be listed on the top of the search results page, the posts whose titles contain all our search words follow, the posts whose titles contain any of our search words next, then the posts whose  content contain the exact string of our input listed at the end. So basically, WordPress searches order by title. If you want to change the order of search results, such as order by relevance, or by post type, etc., you should change the code here.

Leave a Reply