I am just trying to understand how coding in Wordpress works. I have read a bunch of tutorials about post loops and how to modify them with queries, but I have no idea how individual page content I create with Wordpress using a template from a theme is integrated with that, so that my page is displayed in the end, with any posts I want to see there formatted the way I specified it.

Like e.g. I am building a page using Wordpress' twentytwentyone theme's "index.php" template. It just contains a simple post loop. How is the stuff I created (with specific elements) and layout incorporated into that? How does the code in the theme get put together with my page design and elements I put on that page?

To put it differenty: I add a new page in Wordpress, tell Wordpress which template to use, and create some Wordpress blocks and elements in that page. How does that all get merged together? Does Wordpress "magically" put my page code in the post loop body from the template?

What happens if I create and use a template with multiple post loops? Like let's say I have two different post categories, and I want to display the posts from the first category somewhere up my page, and posts from the second category further down?

Also, how would I go about showing e.g. 10 posts of a category, and when the user clicks some "next" button, show the next 10, and so on?

And let's say I put some php code in my page to format the posts, how does that code get fed with the posts from the post loop? twentytwentyone's index.php's post loop already calls the_post() and get_template_part(...). According to the Wordpress codex, the_post() simply "interates the post index in the loop". Does that mean that the data of the post is just made accessible post by post via codex functions when looping through all posts, and I can access the current post's data in my php code (or whatever page element I used)?

All the docs and tuts I found only throw some bits and pieces of info at you, but none I have found gives me the whole picture, like the complete basic Wordpress architecture with control and data flows.

  • 1
    I've tried to write an answer, but the scope of your question is huge, and it contains lots and lots of questions. We have a 1 question per question policy here, if you have 5 questions, open 5 questions rather than asking them all at once. Remember, this isn't a discussion forum, you need to ask a specific question and be able to mark an answer as the factually correct answer, not just the reply you found most useful
    – Tom J Nowell
    Jun 16, 2021 at 9:45
  • I thought so. The problem is that what I can currently see of the entire subject is a huge and wide field of topics and knowledge, and I don't really know where to start. I will try to split this into several questions. Jun 16, 2021 at 10:19
  • If you're just trying to start, you shouldn't be worrying about any of this. It's not the right place to start. I suggest starting with a theme, and going from there: developer.wordpress.org/themes Jun 16, 2021 at 12:12
  • I don't really know why you are recommending this. I have been working with Wordpress for a while, and I want to dig deeper and particularly into coding stuff for it myself. Jun 16, 2021 at 12:31
  • @karx11erx by trying to understand everything you've made things so generic they no longer apply to your actual problem. If you instead ask about the specific problem you have that you are trying to research a solution for, then people can write an answer and guide you in the right direction. Be very specific about what you ask here
    – Tom J Nowell
    Jun 16, 2021 at 13:18

1 Answer 1


How a posts template gets loaded:

  1. You visit a WP site at a URL, /abc/xyz
  2. WP doesn't process pretty permalinks directly, it translates them into the form index.php?var=value internally, so it runs them through all the rewrite rules until the first one matches, if none match, it's a 404 and 404.php is loaded.
    • This is where pretty permalink URLs come from.
  3. The rewrite rule maps /abc/xyz into a URL of the form index.php?foo=xyz where foo is a query variable, the kind that goes into a WP_Query. foo is just an example, for a list of valid parameters see the WP_Query documentation
    • This WP_Query object is the main query that powers the post loop.
    • The user and browser never actually see this, there are no redirects or "rewrites" in the classic sense.
  4. WP looks at this query and makes some decisions, is it a post archive? is it a page? a singular post? It then uses this to setup flags that power is_single() or is_search()
    • This also means that all URLs in standard WordPress on the frontend have a post loop/query, and are either archives of multiple posts, or single post queries
  5. Run some filters, pre_get_posts, etc, so that the query can be changed before stuff happens
  6. To the database! Fetch the posts!
  7. Now that we have posts, use that WP_Query and all the parameters and flags to decide which template to load. ( see the template hierarchy diagrams ). This is how WP will decide which theme file displays the post loop.
    • Notice that the posts have already been fetched from the database.
    • index.php is the fallback which is why it's the only mandatory PHP file in a theme.
    • you can't change the main query from inside a theme template because the main query has already happened, it's too late
  8. Load the theme template we decided on in the previous step. This template should have a standard post loop, and this is what displays the posts, through functions such as the_title and the_content.
    • Notice that the post loop does not fetch posts from the database, that happened before WP even chose the template. It is just providing you an opportunity to display the information

The ultra short version:

  • URL + rewrite rules = query vars
  • main query = new WP_Query( query vars from previous step )
  • look at main query and pick a template based on it, e.g. if main query->is_search() pick search.php, etc
  • load template to display the posts that got fetched

Post content and a theme may enqueue some javascript, and post content itself is taken from the database, with some filters ran over it. There is also the shortcode system and the dynamic blocks system. Each of those are independent systems in their own right.

Also remember that at the end of the day the final result of a block is still just HTML content that gets saved in the post content column of the database.

This is the basic high level architecture of a page load.

  • Oh. My. Goodness. That is even more complicated than I was afraid it would be. Where in that list is the first point of time where I can interfere? Only after 8 has been completed? Or at 5? When I use the twentytwentyone standard template in my Wordpress page and the page is empty, no posts are displayed. That would be because the template file only calls the_post(), get_template_part() (whatever that loads) and twenty_twenty_one_the_posts_navigation()? It is unclear to me how the code in the template interacts with my individual (custom) page content. Jun 16, 2021 at 10:38
  • Is there a way to see the actual Wordpress URL that is created when I simply call a page from my website? Because when I invoke such a page, I never see a link like "index.php?..." appear, not even for a split second. Just trying to understand. Jun 16, 2021 at 10:46
  • It's unclear to me what kind of interference you're wanting to do, your question was extremely.... broad, and you've been very vague and ambiguous about what it is you want to achieve. It's extremely difficult to figure out what it is that you actually want. You also will never see a link such as index.php? unless you turn off permalinks, this is an internal thing, the browser never actually visits those URLs
    – Tom J Nowell
    Jun 16, 2021 at 13:15
  • And you did not mention anything to do with a template that loaded no posts, this is very confusing, you have not shared this information.
    – Tom J Nowell
    Jun 16, 2021 at 13:23

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.