Better to make new wp_query or only modify main query each time?

I think that you are making some confusion between main query and secondary queries in WordPress. Maybe read this can help you to better understand.

In short when you visit an url of your site, that url make wordpress fires a query on db: this is the main query.

Thanks to the action pre_get_post you can modify the main query, e.g. adding, removing or edit query vars before the query is runned on db.

But once the query fired, the only chance to recreate a query that you can call ‘main’ 1 (even if is not triggered by url) is use query_posts, but this practice is highly not recommended.

So if your question is: “Should I use new WP_Query instances for multiple queries or should I use multiple query_posts calls?”

The answer is, surely, new WP_Query instances.


1 You can call ‘main’ the query runned by query_posts, because the main query, is characterized from 2 aspects (besides the fact that normally is triggered by the url):

  1. It’s an object (an instance of WP_Query more precisely) named $wp_query in the global scope. (You can access to it using global $wp_query;)
  2. You can cycle the posts handled by this query using the loop.

Once the query you obtain using query_posts have both these characteristics, it is fledged the main query, even if is not triggered by url.

Leave a Comment