Make WordPress Core

Opened 10 years ago

Closed 8 years ago

Last modified 7 years ago

#17662 closed defect (bug) (fixed)

PHP Notices in get_body_class() for certain query conditions

Reported by: dd32 Owned by: nacin
Milestone: 3.5 Priority: normal
Severity: normal Version: 3.2
Component: Query Keywords: has-patch
Focuses: Cc:


get_body_class() uses get_queried_object(), however, expects that the returned object will be the same as the conditional it's testing.

For example: /author/admin/?cat=1 triggers is_author() and is_category(), however, queried_object() will be a Category object.

get_body_class() checks to see if is_author() before is_category() and as a result, attempts to use a Category object as a Author object.

The solution here is to have the same setting order in all locations (ie. In the code that sets queried_object, and code which iterates through conditionals and then expects to use the queried object).

Another alternative is to call get_userdata() / get_term() instead of relying upon the queried object.

Relying upon the queried object has the obvious downside at present that it can only contain a single object, whereas, WordPress can recieve a request made up of "multiple objects".

Attachments (1)

validate-body-class-vars.diff (3.0 KB) - added by wonderboymusic 8 years ago.

Download all attachments as: .zip

Change History (12)

#1 follow-up: @mfields
9 years ago

I think that the best solution to this might be to extend get_queried_object() to accept a parameter for "type". This way we can specify which queried object is returned in a multiple object query. We would be able to do get_queried_object( 'author' ) or get_queried_object( 'category' ). Perhaps a new function could be created named get_queried_objects() That would return an array of all objects returned by the query. Not sure what to do with get_queried_object_id() though. Perhaps it could always return the ID of the first object in the array returned by get_queried_objects(). Care could be taken to ensure that the first object in this array would always represent the item WordPress now sees as the queried object.

#2 @mfields
9 years ago

  • Cc michael@… added

#3 @dd32
9 years ago

I like that idea mfields, It does push home the fact that often, it's not a single queried object, rather an archive of multiple object intersections.

However, it still leaves it open for a category intersection, does it return CatA, or CatB? or both?

#4 @mfields
9 years ago

In the case of a multiple term query, it might make sense for get_queried_object() to return the first term that appears in the query string. I'm not sure that returning more than one object from get_queried_object() would be a good idea.

In a scenario where Author, CatA and CatB where all queried simultaneously and the developer just needed the category objects, it might be a good solution to allow get_queried_objects() to accept a 'type' parameter as well. A developer could then use get_queried_objects( 'category' ) to return all queried categories in the order they were defined in the url.

#6 @SergeyBiryukov
8 years ago

Closed #20860 as a duplicate.

#7 in reply to: ↑ 1 @arieputranto
8 years ago

Replying to mfields:

Care could be taken to ensure that the first object in this array would always represent the item WordPress now sees as the queried object.

+1 for this.

I myself temporarily redirect user to a search result page for such multiple queries.

#9 @wonderboymusic
8 years ago

  • Keywords has-patch added
  • Milestone changed from Awaiting Review to 3.5

Similar patch to #20519, will prevent notices from being thrown and weird half-empty classes from being generated

#10 @nacin
8 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 22451:

Sanity checks in get_body_class() to ensure we are operating on the type of queried object that we expect. props wonderboymusic. fixes #17662.

#11 @chriscct7
7 years ago

I believe this still happens, or at least the #20519 subsidiary of this ticket. Opened a new ticket here: http://core.trac.wordpress.org/ticket/25398

Note: See TracTickets for help on using tickets.