WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28382 closed enhancement (invalid)

WP_Query - Main Query Flag

Reported by: Howdy_McGee Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9.1
Component: Query Keywords:
Focuses: template Cc:

Description

As I develop more and more WordPress I find myself with 2 or more WP_Queries throughout my templates, sometimes not even having a "Main Query". My request is to add a flag to WP_Query so that you can set it to become a "Main Query" instead of always being a "Secondary Query" by default. This will make things easier to use when targeting a specific query of a specific post type, even if you have multiple queries of that post type used in your template.

"main_query => true"

Default will be false.

Change History (5)

#1 follow-up: @SergeyBiryukov
7 years ago

  • Focuses accessibility administration removed

Main query should be modified using pre_get_posts action. WP_Query should only be used for additional queries.

WP_Query already has is_main_query() method, there is also a conditional function with the same name.

#2 in reply to: ↑ 1 @Howdy_McGee
7 years ago

Replying to SergeyBiryukov:

You don't think there's any situation where you would want to replace the main query with a custom WP Query?

#3 follow-up: @nacin
7 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

query_posts() is already there to replace the main query, exactly what you're requesting. It's just discouraged because "replacing" the main query causes lots of quirks. But also, by the time the template is run, the main query has already run — which means you're doing twice the work with absolutely no benefit.

#4 in reply to: ↑ 3 ; follow-up: @Howdy_McGee
7 years ago

Replying to nacin:

query_posts() is there to replace the main query, but if it's slow(er than WP Query) to use and widely discouraged, why is it the only option to replace the main query? Why are there no alternatives?

#5 in reply to: ↑ 4 @ericlewis
7 years ago

Replying to Howdy_McGee:

query_posts() is there to replace the main query, but if it's slow(er than WP Query) to use and widely discouraged, why is it the only option to replace the main query? Why are there no alternatives?

As @SergeyBiryukov mentioned, pre_get_posts is the best way to modify/replace the main query. Have a read, play around with it, and if that doesn't fit your situation, explain in detail your use case and why pre_get_posts doesn't work for it.

Note: See TracTickets for help on using tickets.