Have you ever asked yourself what happens when you hit a URL on a WordPress website? Here’s the very simplified version of the story: WordPress environment is loaded (core, plugins, theme) WordPress looks at URL and builds some query arguments based on it Obtained query arguments are used to run a WP_Query (known as "main query") Based on the "type" of the query (eg. "single", "archive"…), WordPress chooses a template file among the ones available in theme Template file is loaded to display page There are thousands of lines of code executed in this process, but in this article I’ll concentrate on what happens in points 2 and 3 in the list above. The "main" WordPress function The process in which WordPress sets main query arguments according to current URL is the very core of WordPress operation. The class responsible to perform these tasks is named WP is proof of that, and the method where everything happens is named main(). That method is quite simple, below you can see its entire code: public function main($query_args = '') { $this->init(); $this->parse_request($query_args); $this->send_headers(); $this->query_posts(); $this->handle_404(); $this->register_globals();
Share This