WordPress stores pages and posts in a MYSQL database.
When you request a web page or post from a WordPress website WordPress needs to query the database to retrieve that page/post.
The database query is a collection of MYSQL commands, and must be constructed before the query is executed.
WordPress uses the url that the user types into his web browser to create the MYSQL database query.
The database query retrieves the requested web page from the database.
Because a database query is used it is possible to create web pages with content from multiple WordPress pages or posts.
A standard WordPress blog will display the last 10 blog posts on the home page. Ref- How WordPress Works
The wp_query Object
The database query is handled by the wp_query object, and access to the query is through this object, and associated functions.
The wp_query object is created early in the WordPress load process and the code is found in the wp_settings.php file.
When the default query is created a copy of the query object is made this copy is used throughout WordPress.
You should note that wp_query and the_wp_query refer to the same object, and the new syntax uses the GLOBALS array whereas the old syntax used the form $wp_the_query=new WP_Query().
Here is the code in the settings.php file:
WordPress tends to hide objects behind functions.
So it isn’t common to access the wp_query object directly, but instead indirectly using a function.
For Example, the function the_post() which is found in all templates is just another way of writing $wp_query=>the_post(), which is a standard object access method.:
The actual function is shown below:
The WordPress wp_query object is a global object variable,, and so it is available for use anywhere in WordPress.
WordPress keeps a copy of the wp_query object in wp_the_query.
This copy is used to reset the query back to the default query when creating multiple loops.
Using the WP_Query class means that you don’t need to understand the SQL query syntax as it is taken care of the the WP_Query class.
The Default or Main Query
The default/main query is created by WordPress from the url being requested by the visitor.
A page request for the url.
is a request for the learning-about-wpquery page.
WordPress needs to retrieve this page from the database, and so it builds a query using the information contained in this url.
The query parameters are created by wp() function and stored in an array,in the wp_query object.
These query parameters can be modified before the query takes place. -Using the pre_get_posts filter.
The default database query occurs when the $wp_query object is created which is loaded very early in the WordPress load process.
If you are requesting a standard WordPress home page configured as a standard blog then the query object ($wp_query) will contain the last 10 blog posts, and associated meta data.
If you are requesting the about me page then the query object ($wp_query) will contain the contents of the about me page, and associated meta data.
Global Query Variables
As part of the main query WordPress uses a number of global variables to maintain information about the query, and these query variables are used by functions like have_posts(), the_posts() etc that are part of the normal WordPress loop.
One of the main considerations when creating custom loops is how these variables are effected.
Overview of Core Loop Functions
First let’s start with a Simple View of the Loop-–
The results of the database query are stored in the wp_query object as an array of objects. The loop just processes each element of this array.
There are two main core fumctions used in the loop have_posts() and the_post().
have_posts() — This checks if we have any more posts in the loop to display. It queries a counter (post_count variable) in the wp_query object. Returns false when the counter is zero and resets (rewinds) the loop at the end.
the_post() — gets the content of the current post from the wp_query object and stores it in the $post object. It then increases the $post_count variable.
When you open a WordPress page/post in a web browser you initiate a database query to retrieve the contents of that post/page.
This database query is known as the default or main query.
WordPress stores the query parameters, and the results of the query in a global object called wp_query.
Many WordPress core functions like have_posts and the_post use and modify this object.
Now we understand more about the Query it’s time to look at creating custom queries. —
Related Tutorials and Resources:
- How WordPress Works
- The WordPress Load Process-Beginners Guide
- Understanding the WordPress Loop
- Beginners Guide to WordPress Templates
- WordPress functions and Functions File