Wednesday, July 13, 2022
HomeWordPress Developmentloop - How does WP generate the default $question in WP_Query primarily...

loop – How does WP generate the default $question in WP_Query primarily based on the URL?


It occurs in 3 principal levels:

  1. The URL is parsed by WP::parse_request() that is finished by matching the URL to WordPress’s rewrite guidelines. You possibly can see these guidelines within the rewrite_rules choice within the wp_options desk, or use a plugin
    like Rewrite Guidelines Inspector to browse them. This ends in parameters for WP_Query.
  2. The parsed parameters are handed as arguments to the ‘principal’ world occasion of WP_Query, which queries the database for posts. The Loop is for looping by means of the posts returned by this question.
  3. Primarily based on the arguments handed to the WP_Query occasion numerous properties resembling is_single or is_archive are set on the question object by WP_Query::parse_query(), and primarily based on these properties wp-includes/template-loader.php will load a template file out of your theme. How templates are selected, i.e. the template hierarchy, is visualised right here.

For many day-to-day improvement solely step 3 is absolutely related, and it must be pretty apparent from the URL what posts are going to be loaded:

  • /my-page The web page with the my-page slug.
  • /class/my-category Posts within the my-category class.
  • /2020/03/01 Posts from March 1st 2020.
  • ?s=my+search+time period Posts matching the search question “my search time period”.

You possibly can normally infer all these URLs primarily based on what the View hyperlinks for numerous forms of content material within the admin space level to.

Whenever you register customized put up sorts and taxonomies there will likely be a rewrite argument that permits you to outline what the URL to an archive of these posts will likely be. Discuss with the documentation for a way these work.

The vital factor to notice is that by default, other than the 404 template, all URLs created by WordPress will load both a single put up, or an inventory of posts.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments