Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

My assumption is that the main loop is always the first loop and you're always supposed to use query_post? Is this correct? Then whenever you you need to feed in some arguments to get a different set or order of posts you use WP_Query or get_posts. These would both be secondary loops? I just feel I'm hardly interacting with the Main Loop at all so I have this feeling, whilst everything works, I'm just not doing things properly.

share|improve this question
    
Not sure if your questions is clear. Could you please elaborate? –  kaiser Aug 20 '13 at 9:50
add comment

1 Answer 1

up vote 5 down vote accepted

When you visit a link of your site, WordPress recognizes that link and understands (using the rewrite rules you have defined or using the query vars in the URL if there are no pretty permalinks) what is the request of the URL.

After that WordPress, using this request, run a query. This is the main query. So, the main query is the one that is triggered by the URL of the page.

It can seems strange, but main query may not even be the first query. This because WordPress does a lot of things before running the main query: include plugins, include theme and also fire some actions and filters.

So if a plugin or the theme hook into one of this hooks and run a query this one is effectively run before the main query.

After the main query run, WordPress includes the appropriate template file. Which is the appropriate file is defined by the template hierarchy.

So when template file is loaded the main query is already run and you can make use of it directly using the loop.

If you use query_posts in a template file this cause the main query run again— slows down the page load.

This is the reason why, if you need to change the main query, is better using pre_get_posts hook that let you alter the query before is runned, so without run it again.

Be aware that pre_get_posts runs for every query, and not only for the main one, but using is_main_query in the function that hooks into that action you can control only the main query.

As you seems to know WordPress gives possibility to run different queries from the main and you have different alternatives to do this thing, the most popular and the suggested one is to use a new instance of WP_Query.

Now, every query that is not the main is a secondary query (even if is runned before the main query, as said above).

share|improve this answer
3  
+1. In short, the main query is the one that is output by when calling if ( have_posts() ) : while ( have_posts() ) : the_post();. Secondary loops are any loops output via WP_Query() or get_posts(). And never use query_posts(), for any reason, ever. –  Chip Bennett Aug 20 '13 at 11:59
    
+1 for your comment because "never use query_posts()" is not enough clear in my answer. –  G. M. Aug 20 '13 at 12:27
    
@G.M. thank you for your very detailed answer :) –  UzumakiDev Aug 20 '13 at 13:09
1  
@KyleJoseph you're welcome. :) –  G. M. Aug 20 '13 at 13:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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