Make WordPress Core

Opened 12 years ago

Closed 11 years ago

Last modified 6 years ago

#21394 closed defect (bug) (fixed)

query.php: get_queried_object() result cannot be assumed to be non-NULL

Reported by: lkraav's profile lkraav Owned by: wonderboymusic's profile wonderboymusic
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.3.2
Component: Warnings/Notices Keywords: has-patch
Focuses: Cc:

Description (last modified by SergeyBiryukov)

I'm dealing with a WPMUdev plugin called MarketPress. Its /store/products/ page is a special CPT catalog thing that seems to have wp_title() issues, so I dug into it.

Turns out that:

is_front_page() -> is_page() -> $page_obj = $this->get_queried_object() -> $page_obj == NULL

But is_page() (and other is_...()) go harrassing $page_obj assuming there's always something useful there. This creates a flood of "non-object access" notices.

I would expect something like this to make sense in these is_...() functions:

if (!is_null($page_obj)) return false;

Since WP_Query::get_queried_object() first sets its result as null and returning a value is actually conditional, I don't see a way around these checks. Enlighten me please if I'm wrong.

I've looked at #19035, #18614, #17662. All of them are situations where there's at least something there in $page_obj. None of them seem to discuss or patch NULL values.

I discovered this on 3.3.2, but todays trunk trunk/wp-includes/query.php@21248#L3340 has the same code.

Attachments (4)

is-page-bail-on-null.diff (343 bytes) - added by wonderboymusic 12 years ago.
21394.diff (1.9 KB) - added by ryan 12 years ago.
21394.general-template.diff (1.1 KB) - added by markoheijnen 12 years ago.
Fixes for general-template.php
21394.2.diff (5.0 KB) - added by wonderboymusic 11 years ago.

Download all attachments as: .zip

Change History (21)

#1 @lkraav
12 years ago

  • Cc leho@… added

Bleh. Ignore the "patch" above.

if (is_null($page_obj)) return false;

#2 @SergeyBiryukov
12 years ago

  • Description modified (diff)

#3 @wonderboymusic
12 years ago

  • Component changed from Query to Warnings/Notices
  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 3.5
  • Owner set to wonderboymusic
  • Status changed from new to accepted

This is a simple check for empty

Last edited 12 years ago by wonderboymusic (previous) (diff)

#4 @ryan
12 years ago

Related: #20519

@ryan
12 years ago

#5 @ryan
12 years ago

Empty check for all is_*() funcs.

#6 follow-up: @ryan
12 years ago

That patch adds a bunch of defensive checks, but we need to better understand why a template tag is true but its corresponding queried object is empty.

#7 @ryan
12 years ago

  • Keywords early added
  • Milestone changed from 3.5 to Future Release

Per bug scrub, punting so we can take a closer look and not just patch symptoms.

@markoheijnen
12 years ago

Fixes for general-template.php

#8 @markoheijnen
12 years ago

  • Keywords early removed
  • Milestone changed from Future Release to 3.6

in wp-includes/general-template.php there was also a case where the assumption was made that get_queried_object wasn't null.

#9 in reply to: ↑ 6 @lkraav
12 years ago

Replying to ryan:

That patch adds a bunch of defensive checks, but we need to better understand why a template tag is true but its corresponding queried object is empty.

Good thing I did a search for is_page(), I was apparently about to file a duplicate of this same thing.

Here's the scenario that causes the trifecta:

NOTICE: wp-includes/query.php:3348 - Trying to get property of non-object
NOTICE: wp-includes/query.php:3350 - Trying to get property of non-object
NOTICE: wp-includes/query.php:3352 - Trying to get property of non-object

This e-commerce plugin uses virtual pagename's for rewriting. /store/products/ gets translated to pagename=product_list request:

$new_rules[$this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->products') . '/?$'] = 'index.php?pagename=product_list'; 

Then it has a wp action, that checks for query_var == "product_list" and builds up a "real" query with a query_posts call.

Now Yoast's WP SEO plugin is actually the one causing these notices, because for some reason it does wp_reset_query() in its wp action $this->metadesc() handler. Why reset the query here, I do not know, I'm not familiar at all with the innards of this plugin. Then WP SEO hits the system with is_front_page() etc that passes through is_page() with $wp_the_query (i.e. the original "virtual" pagename) as its target.

So I am not sure how much understanding here is needed, but it seems real world is somehow coming up with these ways to do things that causes get_queried_object() to be null. It doesn't feel like what folks are doing is too much off The Right Way either. Or...?

Last edited 12 years ago by lkraav (previous) (diff)

#10 @ryan
12 years ago

  • Milestone changed from 3.6 to Future Release

#11 @wonderboymusic
11 years ago

  • Milestone changed from Future Release to 3.7

Marko's patch had a few more stragglers, I found even more upon closer examination

#13 @wonderboymusic
11 years ago

#17427 was marked as a duplicate.

#14 @wonderboymusic
11 years ago

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

In 25310:

Make sure the queried object is non-null before accessing its properties.

Props markoheijnen, ryan.
Fixes #21394.

#15 @SergeyBiryukov
11 years ago

#24788 was marked as a duplicate.

#16 @danbrellis
9 years ago

I know this ticket is set to fixed, and I apologize if I'm not following protocol for bringing up a related issue, but I just had this problem and was able to trace it back to WP_Query::is_singular()

In my case the plugin bbPress is making calls to is_singular() and when I'm viewing an RSS Feed, WP_Query::get_queried_object() returns NULL. However, WP_Query::is_singular() still checks if the result of get_queried_object() has the property post_type.

The error I'm getting in my RSS is <b>Notice</b>: Trying to get property of non-object in <b>C:\Apache24\htdocs\bp-dev\wp-includes\query.php</b> on line <b>4654</b><br />

I would assume applying a similar fix to WP_Query::is_singular() would suffice?

public function is_singular( $post_types = '' ) {
  if ( empty( $post_types ) || !$this->is_singular )
    return (bool) $this->is_singular;

  $post_obj = $this->get_queried_object();
  if (is_null($post_obj)) return false;
  return in_array( $post_obj->post_type, (array) $post_types );
}
Note: See TracTickets for help on using tickets.