1

I understand the mechanisms of creating a custom query by instantiating a new WP_Query object when I need granular control of what posts I'd like to retrieve. However, after searching through the documentation for longer than I'd like to admit, I cannot find an explanation on how WP core determines how to construct the default query for a page. So that is to say, if a template file is loaded and immediately jumps into The Loop, how can I know what posts WP has retrieved for me? I would like to find a clearly defined explanation or algorithm available in the documentation - the fact that I haven't makes me wonder if I'm misunderstanding something about the "default" query.

1 Answer 1

6

It happens in 3 main stages:

  1. 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_rules option in the wp_options table, or use a plugin like Rewrite Rules Inspector to browse them. This results in parameters for WP_Query.
  2. 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.
  3. Based on the arguments passed to the WP_Query instance various properties such as is_single or is_archive are set on the query object by WP_Query::parse_query(), and based on those properties wp-includes/template-loader.php will 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-page The page with the my-page slug.
  • /category/my-category Posts in the my-category category.
  • /2020/03/01 Posts from March 1st 2020.
  • ?s=my+search+term Posts 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.

5
  • Thank you for your answer. This gives me a lot to explore. How does WP::parse_request() deal with permalinks, which hide a lot of those details in the URL? Jul 13, 2022 at 18:04
  • As I mentioned it uses the rewrite rules. If you look at them using the tools I mentioned you can see that they are regexes that match parts of the URL to query parameters. Jul 13, 2022 at 18:37
  • Yes, after I left that question I looked into how server rewrites work (pmg.com/blog/…) (daggerhartlab.com/wordpress-rewrite-api-examples). I'm assuming once you've registered a permalink it gets added into the rewrite rules through the rewrite API so the back end knows what to serve up. Jul 13, 2022 at 18:48
  • The rewrite rules only need to be updated if your permalink structure gets updated, such as the settings in Settings > Permalinks, or if a new post type is registered. You don't need specific rules for individual categories or posts since those are parsed out of the URL using the rule. Jul 13, 2022 at 18:51
  • I apologize. I should have clarified. I know that every resources has a permalink. I was referring specifically to "pretty permalinks" that remove all of the query parameters from the URL. Jul 13, 2022 at 19:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.