WPSolr logo
Search
Close this search box.

Table of contents :

How WP_Query performs a WordPress search

WP_Query Wordpress search

Table of contents :

WordPress relies on the WP_Query class to perform searches (and load data in general). This class is a very versatile and customizable piece of PHP code since it includes many WordPress hooks and this way many plugins have been built around it. Without WP_Query, adding an efficient and relevant WordPress search would be very difficult.

 

This guide will overview the components of the WP_Query class, among others, that form the search functionalities within WordPress.

 

Image diagram_the_standard_WordPress_SQL_search_1.gif of How WP_Query performs a WordPress search

 

For the purpose of this guide, let’s consider that the user enters the term “shirt” in the WordPress search bar.

 

The user enters the term "shirt" in the WordPress search bar.

 

This will reload the page. It will now have the following url : https://www.yourwebsite.com/?s=shirt.

 

This way the term you want to search (in this case “shirt”) is sent to the server.

 

WordPress creates the SQL search query using WP_Query

 

In the “wp-includes/class-wp-query.php” file, the WP_Query->get_posts() method is the one most responsible for creating the SQL query.

 

This lengthy PHP method will fetch the term that the user entered in their search in the $this->query array. In this case, the “s” ($this->query[“s”]) value is “shirt”.

 

SQL for finding matching items (using WP_Query->parse_search())

 

The WP_Query->get_posts() method invokes WP_Query->parse_search() to generate the portion of the SQL query responsible for locating matching items within the wp_posts table of the database.

 

In the screeenshot down below, we can see the SQL returned by this WP_Query->parse_search() method :

PHpstorm screenshot : SQL query to find matching elements using term "shirt"

 

This is then added to the $where variable for later use :

$where .= $search . $whichauthor . $whichmimetype;

 

Completion of the SQL query (WP_Query->request)

 

Again, in the same WP_Query->get_posts() method, the following will create the final version of the SQL query that is used for WordPress search :

$this->request = "
  SELECT $found_rows $distinct {$wpdb->posts}.ID
  FROM {$wpdb->posts} $join
  WHERE 1=1 $where
  $groupby
  $orderby
  $limits
";

 

As we can see, the $where variable, that contains the LIKE operators for our search term (“shirt”), is then added to the final SQL query.

 

WordPress will execute it in the ‘wpdb’ class :

$post_ids = $wpdb->get_col( $this->request );

 

WordPress runs the SQL query using wpdb

 

In the “wp-includes/class-wp-db.php” file containing the wpdb class, the _do_query() method is called.

 

Inside it, the following line is present. It will run the SQL query previously created :

$this->result = mysqli_query( $this->dbh, $query );

 

The “$query” value to return the relevant items for the word “shirt” is the following :

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 
AND (((wp_posts.post_title LIKE '%shirt%') OR (wp_posts.post_excerpt LIKE '%shirt%') OR (wp_posts.post_content LIKE '%shirt%'))) 
AND ((wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')) 
OR (wp_posts.post_type = 'page' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'attachment' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'))
OR (wp_posts.post_type = 'e-landing-page' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')) OR (wp_posts.post_type = 'product' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private'))) 
ORDER BY wp_posts.post_title LIKE '%shirt%' DESC, wp_posts.post_date DESC LIMIT 0, 10;

 

The WP_Query->get_posts() will then return the items that have the following ids:

WP_Query : returned post ids

 

We can test it out for ourselves by entering the same query in phpMyAdmin. As planned the same items are return in the correct order :

PypMyadmin SQL query for "shirt" items.

 

Now that the results have been fetched, the template can be loaded for the user :

Wordpress default search results for term "shirt".

 

 

Related posts ... not powered by WPSOLR 😊