loop – How does WP generate the default $query in WP_Query based on the URL?
It happens in 3 main stages:
- The URL is parsed by
WP::parse_request()this is done by matching the URL to WordPress’s rewrite rules. You can see those rules in the
rewrite_rulesoption in the
wp_optionstable, or use a plugin
like Rewrite Rules Inspector to browse them. This results in parameters for
- The parsed parameters are passed as arguments to the ‘main’ global instance of
WP_Query, which queries the database for posts. The Loop is for looping through the posts returned by this query.
- Based on the arguments passed to the
WP_Queryinstance various properties such as
is_archiveare set on the query object by
WP_Query::parse_query(), and based on those properties
wp-includes/template-loader.phpwill load a template file from your theme. How templates are chose, i.e. the template hierarchy, is visualised here.
For most day-to-day development only step 3 is really relevant, and it should be fairly obvious from the URL what posts are going to be loaded:
/my-pageThe page with the
/category/my-categoryPosts in the
/2020/03/01Posts from March 1st 2020.
?s=my+search+termPosts matching the search query “my search term”.
You can usually infer all these URLs based on what the View links for various types of content in the admin area point to.
When you register custom post types and taxonomies there will be a
rewrite argument that lets you define what the URL to an archive of those posts will be. Refer to the documentation for how those work.
The important thing to note is that by default, apart from the 404 template, all URLs created by WordPress will load either a single post, or a list of posts.