Opened 13 years ago
Closed 11 years ago
#25680 closed defect (bug) (fixed)
is_main_query() _doing_it_wrong() notice suggests using WP_Query statically
| Reported by: | dd32 | Owned by: | wonderboymusic |
|---|---|---|---|
| Priority: | normal | Milestone: | 4.4 |
| Component: | Text Changes | Version: | |
| Severity: | normal | Keywords: | has-patch commit |
| Cc: | Focuses: |
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 (2)
Change History (14)
#3
@
13 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.
#4
@
13 years 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
#5
@
13 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
@
13 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.
#8
@
13 years ago
I could see doing something like the this:
In
pre_get_posts, the standaloneis_main_query()function should not be used. Use the passedWP_Queryobject'sis_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.
#10
@
11 years ago
- Keywords 2nd-opinion close removed
- Milestone Awaiting Review → 4.4
- Owner set to
- Status new → assigned
you decide if we want this open or closed
#11
@
11 years ago
- Keywords has-patch commit added
- Owner changed from to
@nacin's suggestion for instead using WP_Query->is_main_query() is the most elegant solution here. This is done in 25680.2.diff.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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.