Exercise 2: Why have_posts()/the_post() "Just Work" With No Query-Writing Code — Possible Solution ==================================================================== WHAT THIS CHAPTER SAYS ABOUT WHERE "THE CURRENT QUERY" COMES FROM ------------------------------ Per this chapter, "before your template file ever runs, WordPress has already built a query based on the requested URL - visiting a category archive automatically queries that category's posts, visiting a single post automatically queries just that one post." This automatic querying happens as part of WordPress's own request-handling process, entirely before the chosen template file (per the hierarchy from WordPress Intermediate/Advanced 2) is ever included and run. WHY have_posts() AND the_post() DON'T NEED AN EXPLICIT QUERY IN single.php ------------------------------ Per this chapter, "have_posts()/the_post() simply work in a single/archive template with no query-writing required - the query already happened." Both functions operate against WordPress's own already-built "main query" by default, rather than requiring the template itself to specify what to query for. Since the visitor requesting a specific single post already caused WordPress to automatically build a query matching just that post before single.php was ever reached, the template's own Loop can simply ask "are there more posts in the CURRENT (already-existing) query?" without ever needing to construct that query itself. WHY THIS IS DIFFERENT FROM THE WP_Query SCENARIO ELSEWHERE IN THIS CHAPTER ------------------------------ This chapter's own WP_Query material covers exactly the case where the automatic main query ISN'T sufficient - when a template needs posts beyond what WordPress already queried (a "related posts" list, for example). In that case, a query genuinely has to be written explicitly, because no automatic query for "5 related posts in the news category" exists on its own. But for an ordinary single.php simply displaying the one post the URL is actually requesting, the automatic main query already covers exactly what's needed, so no additional query-writing is required at all. WHY THIS WORKS AS AN ANSWER ------------------------------ It cites this chapter's own explanation of the automatic pre-template query directly, explains why that removes the need for explicit query-writing in an ordinary single/archive template, and distinguishes this case from the chapter's own WP_Query example, where an explicit query genuinely is required because the automatic one isn't sufficient.