Make WordPress Core

Opened 10 years ago

Closed 8 years ago

#25680 closed defect (bug) (fixed)

is_main_query() _doing_it_wrong() notice suggests using WP_Query statically

Reported by: dd32's profile dd32 Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version:
Component: Text Changes Keywords: has-patch commit
Focuses: Cc:


At present when is_main_query() is called within the pre_get_posts hook, it returns a _doing_it_wrong() error which refers to replacing it with WP_Query::is_main_query(). Obviously it's saying call the method on the WP_Query object, but some people can take that literally and call it statically.

Notice: is_main_query was called incorrectly. In pre_get_posts, use the WP_Query::is_main_query() method, not the is_main_query() function.

We should instead, make mention to checking the passed $query object via $query->is_main_query() instead somehow.

Example of how it was incorrectly used:

if (( is_front_page() || is_home()) && WP_Query::is_main_query())
	$query->set('post_type', array('wp_meetup_event', 'post'));

Attachments (2)

25680.diff (748 bytes) - added by micahwave 10 years ago.
25680.2.diff (885 bytes) - added by DrewAPicture 8 years ago.

Download all attachments as: .zip

Change History (14)

#1 @rmccue
10 years ago

Maybe something like In pre_get_posts, use the $query->is_main_query() (WP_Query::is_main_query) method, not the is_main_query() function.. Leaves the function name there for those that want it, but removes the () to indicate you're not meant to call it.

#2 @tenpura
10 years ago

How is $passed_query_object->is_main_query()?

#3 @nacin
10 years ago

We could change it from WP_Query::is_main_query() to WP_Query->is_main_query without changing around strings but trying to introduce some clarity.

10 years ago

#4 @micahwave
10 years ago

Replaced WP_Query:: with $query-> since it seems to follow what's written here:

#5 @nacin
10 years ago

The problem is that $query may not be the name of the function argument. That's all up to them. I don't know which would be more usable.

#6 @nofearinc
10 years ago

I would leave it as is, there are other instances in core following that pattern and, while not obvious, they are technically correct.

Several related examples could be found in wp-includes/wp-db.php, both in _deprecated_function calls and PHPDoc, such as:

_deprecated_function( __FUNCTION__, '3.5', 'wpdb::has_cap( \'collation\' )' ); 

Even though global $wpdb is usually set, other instances could be initialized as well.

#7 @nacin
10 years ago

  • Keywords 2nd-opinion close added
  • Milestone changed from 3.8 to Awaiting Review

#8 @DrewAPicture
10 years ago

I could see doing something like the this:

In pre_get_posts, the standalone is_main_query() function should not be used. Use the passed WP_Query object's is_main_query() class method instead. See <URL>.

It probably wouldn't hurt to flesh out that Codex article either, maybe with a section specifically explaining the difference between the function and the method that we could link to.

#9 @ethitter
10 years ago

  • Cc erick@… added

#10 @wonderboymusic
8 years ago

  • Keywords 2nd-opinion close removed
  • Milestone changed from Awaiting Review to 4.4
  • Owner set to DrewAPicture
  • Status changed from new to assigned

you decide if we want this open or closed

8 years ago


#11 @DrewAPicture
8 years ago

  • Keywords has-patch commit added
  • Owner changed from DrewAPicture to wonderboymusic

@nacin's suggestion for instead using WP_Query->is_main_query() is the most elegant solution here. This is done in 25680.2.diff.

#12 @wonderboymusic
8 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 34366:

Docs: is_main_query()'s _doing_it_wrong() notice suggests using WP_Query statically (WP_Query::is_main_query()). Use an alternate syntax: WP_Query->is_main_query(), to not confuse.

Props DrewAPicture, micahwave.
Fixes #25680.

Note: See TracTickets for help on using tickets.