The WordPress Main Query - insight and functionality

As a WordPress developer you should know the basic concepts of the CMS, including the WordPress Main Query. This article gives an insight into the central database query of WordPress and explains how the SQL query is formed.

What is the WordPress Main Query?

I recently explained the structure and structure of the WordPress database . WordPress communicates with the database for reading and writing data using database queries, so-called SQL queries.

When a WordPress site is called up, many different SQL queries are usually carried out, for example to display navigation menus or sidebar widgets. Theme and plugin options also have to be fetched from the database.

What these secondary SQL queries have in common is that they are first made when there is a specific need. They are triggered by certain functions such as menus & widgets or as part of themes and plugins.

In addition, there is always a central database query when WordPress is initialized. This query is known as the main query .

How is the main query formed?

The main query is determined by the URL of the page called in the frontend.

Examples:

  • When calling up an article or page, the main query fetches a single post
  • Calling up a category leads to a query of the last 10 posts from this category
  • Submitting a search form leads to a search query in the database
  • The blog index with / page / 3 leads to the query for posts 20 to 29

WordPress takes the URL and uses the permalink settings to create the arguments for the SQL query. The main query is then executed with these parameters with WP_Query , the central class for all WordPress database operations.

Where is the main query executed?

These steps are roughly simplified when calling up a WordPress website:

  1. The basic WordPress core files are initialized
  2. All activated plugins and the functions.php of the active theme are loaded
  3. The wp () function is executed.
  4. This calls $ wp-> main () , where $ wp is an object instance of the class WP .

Source: WordPress.org

This function main () of the class WP is responsible for the main query:

 public function main($query_args = '') { $this->init(); $this->parse_request($query_args); $this->send_headers(); $this->query_posts(); $this->handle_404(); $this->register_globals(); do_action_ref_array( 'wp', array( &$this ) ); }

In the function () parse_request the URL is processed and the query argument made, executed in query_posts () Main Query. The remaining methods are used to define global variables, create HTTP headers and handle Error 404.

If you want to take a closer look at WordPress routing, you should take a look at the article Life of a Front-end WordPress Request . There it is explained in detail how WordPress generates the query from URL and rewrite rules.

Use Main Query in WordPress

The WordPress Main Query is saved in the global variable $ wp_query and is therefore available everywhere, including in the WordPress theme. There the main query with the WordPress loop of the template file is output and the content is displayed in the front end.

WordPress also selects the template file using the URL, e.g. archive.php for archives or single.php for individual posts. You can find a comprehensive overview in my article about the different template files in a WordPress theme .