Opened 9 months ago
Last modified 9 months ago
#21790 new defect (bug)
When set a static front page WP main query isn't set correctly
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Query | Version: | 3.4.1 |
| Severity: | normal | Keywords: | dev-feedback |
| Cc: |
Description
In my project I use on several places pre_get_posts filter. When setting a static frontpage and blog page I get several notices on my screen. When I var_dump the main query the only value that is set it the page_id.
Even the post_type isn't set.
Change History (11)
comment:2
in reply to:
↑ 1
markoheijnen — 9 months ago
Replying to nacin:
The notices are?
Notice: Undefined property: WP_Query::$post in /var/www/vhosts/*/subdomains/*/httpdocs/wp-includes/query.php on line 2986
Notice: Trying to get property of non-object in /var/www/vhosts/*/subdomains/*/httpdocs/wp-includes/post-template.php on line 1270
Notice: Undefined property: WP_Query::$post in /var/www/vhosts/*/subdomains/*/httpdocs/wp-includes/query.php on line 2986
Notice: Trying to get property of non-object in /var/www/vhosts/*/subdomains/*/httpdocs/wp-includes/query.php on line 3349
Notice: Trying to get property of non-object in /var/www/vhosts/*/subdomains/*/httpdocs/wp-includes/query.php on line 3351
Notice: Trying to get property of non-object in /var/www/vhosts/*/subdomains/*/httpdocs/wp-includes/query.php on line 3353
comment:3
follow-up:
↓ 6
markoheijnen — 9 months ago
In my case I'm using is_page_template() and is_front_page() on the filter. How I look in the code this will only work inside the loop but I can be wrong. However I somehow do expect it to work without the need of a loop.
comment:5
markoheijnen — 9 months ago
For me to see the notices is to set a static blog and front page. and use a function like is_front_page. I'm going to try if I can write an unit-test to make it visible. Not sure yet how to create this situation and where to check on.
function some_random_modification( $query ) {
if( $query->is_main_query() && is_front_page() ) {
}
return $query;
}
add_filter( 'pre_get_posts', 'some_random_modification' );
comment:6
in reply to:
↑ 3
SergeyBiryukov — 9 months ago
is_front_page() calls is_page(), which only works on 'wp' action or later.
'pre_get_posts' is too early to use conditional tags (this is also mentioned on the Codex page).
comment:7
markoheijnen — 9 months ago
Uhm okay, i do think it is wrong tho. 'pre_get_posts' is a place you want to use it since you want to control the main query.
How I can see it now there is no way to do it then. Having said that it does work. I only get some notices.
comment:8
markoheijnen — 9 months ago
Just a little note what I already said: it only shows the notices when static homepage is set. On all other pages it is fine without any notice.
comment:9
SergeyBiryukov — 9 months ago
Well, is_front_page() only calls is_page() when a static home page is set:
http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/query.php#L3287
The notices are coming from get_queried_object(), where $this->post is not filled yet:
http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/query.php#L2986
So $page_obj in is_page() ends up being null:
http://core.trac.wordpress.org/browser/tags/3.4.1/wp-includes/query.php#L3345
comment:10
markoheijnen — 9 months ago
yes, I know that. I looked through the code to see if I could fix it.
I'm now just curious if it is fixable or how you otherwise modify the query on the front page with the use of WordPress code.
comment:11
markoheijnen — 9 months ago
I now solved the issue by using this:
( $query->get('page_id') == get_option('page_on_front') || is_front_page() )
Reason was that is_front_page() doesn't work then in pre_get_posts(). It does work but I'm not a fan of using this.

The notices are?