I have a theme where I need multiple queries for each column. Is it better to modify the main query with pre_get_post and reset the loop or create a new query for each of my columns?
1 comment
Comments are closed.
I have a theme where I need multiple queries for each column. Is it better to modify the main query with pre_get_post and reset the loop or create a new query for each of my columns?
Comments are closed.
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 multiplequery_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):WP_Query
more precisely) named$wp_query
in the global scope. (You can access to it usingglobal $wp_query;
)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.