1

My archive.php starts like this:

        get_header(); ?>
        <?php if( have_posts() ): ?>
            <div id="container" class="portfolio-wrapper">
            <?php while( have_posts() ): the_post(); ?>

I'm curious... where is the actual WP Query made? For example when I press on a Category or on an Archive link, I get to this page, but where's the actual select done?

More specifically, I'm curious where the first query is created (in what file), since the above only uses a query which was already made somewhere.

I'm asking because I want to create a custom archive (using a shortcode with posts ids in it) and I want to decide what is the best approach to do that :).

  • Google "WordPress main query" for the general answer to your general question. For detailed help with your problem please edit the question to include as much detail as you can about what you want to accomplish. – s_ha_dum Oct 10 '15 at 16:35
  • I think my question was pretty clear to begin with. I've edited it further :). The Google search has no useful result. I am not interested in what is the "main query" or The Loop. I know what those are and that information is not useful for making me understand the flow of things under the hood. – Axonn Oct 10 '15 at 18:21
  • "I know what those are." -- No you don't. The "main query" is "where the first query is created". Understand that, you understand most of your question. As far as "no useful result", the second result is the one you want-- including the location of the file(s) involved. – s_ha_dum Oct 10 '15 at 18:25
  • 1
    You are getting slightly different results than I am but I am sure there is one about WP_Query, which handles all of your post, page, archive, single, CPT, etc. queries. At any rate, tracing the function calls from any of the related functions will get you to the same place. – s_ha_dum Oct 10 '15 at 18:39
  • 1
    Everyone calm down. :) This is a pretty good question and I concur that existing info on it kind of tends to suck. – Rarst Oct 10 '15 at 19:45
1

The main query is set up by WP class (curiously little known, since there is rarely reason to mess with it). It happens in between WP core finishing load and proceeding to load template, in wp-blog-header.php.

Note that some nuances of implementation:

  • WP class works on a global, set up earlier in wp-settings.php
  • the global it works on is $wp_the_query, which acts at original instance from which more commonly used $wp_query is spawned. This two-variable implementation is used for detection if the main query had been modified, among other things.
  • I keep stumbling over this question in my own work, and it's probably worth adding: if you are dealing with the actions or filters used by get_posts, there are a number of different wp_query instances which can get used in the context of archive.php (and many of them have nothing to do with the posts which will be displayed). – rdm May 17 '18 at 22:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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