WordPress.org

Make WordPress Core

Opened 6 months ago

Last modified 4 months ago

#25680 new defect (bug)

is_main_query() _doing_it_wrong() notice suggests using WP_Query statically

Reported by: dd32 Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Text Changes Keywords: 2nd-opinion close
Focuses: Cc:

Description

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 (1)

25680.diff (748 bytes) - added by micahwave 5 months ago.

Download all attachments as: .zip

Change History (10)

comment:1 rmccue6 months 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.

comment:2 tenpura6 months ago

How is $passed_query_object->is_main_query()?

comment:3 nacin6 months 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.

micahwave5 months ago

comment:4 micahwave5 months ago

Replaced WP_Query:: with $query-> since it seems to follow what's written here: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

comment:5 nacin5 months 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.

comment:6 nofearinc5 months 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.

comment:7 nacin5 months ago

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

comment:8 DrewAPicture5 months 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.

comment:9 ethitter4 months ago

  • Cc erick@… added
Note: See TracTickets for help on using tickets.