Make WordPress Core

Opened 11 years ago

Closed 11 years ago

#25341 closed defect (bug) (fixed)

multiple custom post types in pre_get_post filter break body_class

Reported by: norcross's profile norcross Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 3.7 Priority: normal
Severity: normal Version: 3.1
Component: Template Keywords: has-patch commit
Focuses: Cc:

Description

Issue: when viewing a custom post type archive template, and having added a pre_get_posts filter to display two separate custom post types on that page, the body_class function breaks trying to process an array to string conversion

Attachments (2)

25341-post-template.php.diff (840 bytes) - added by norcross 11 years ago.
checking for post_type query_var as an array or single
25341.diff (686 bytes) - added by SergeyBiryukov 11 years ago.

Download all attachments as: .zip

Change History (14)

@norcross
11 years ago

checking for post_type query_var as an array or single

#1 @norcross
11 years ago

  • Cc andrew@… added

#2 @SergeyBiryukov
11 years ago

  • Keywords commit added; 2nd-opinion needs-testing removed
  • Milestone changed from Awaiting Review to 3.7
  • Version changed from trunk to 3.1

25341.diff is a simplified version.

#3 @SergeyBiryukov
11 years ago

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

In 25471:

Expect a possible array of post types in get_body_class(). props norcross. fixes #25341.

#4 @nacin
11 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

is_post_type_archive() is not supposed to return true when there are multiple post types being queried. At least, that was my assumption.

Can we investigate how this can happen?

#5 @norcross
11 years ago

the Codex indicates that it'll return a string or array, and says multiple could exist: http://codex.wordpress.org/Function_Reference/is_post_type_archive

based on the docs, it's because you can declare one CPT or check for a whether you are on any CPT archive page.

Last edited 11 years ago by norcross (previous) (diff)

#6 follow-up: @nacin
11 years ago

is_post_type_archive is only set to true when if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {. norcross, you reported this against "trunk" originally. Could anyone actually reproduce this in 3.6? Cause it looks like [25312] broke this. See also #18614 where I suggested a revert.

#7 @kovshenin
11 years ago

  • Cc kovshenin added

#8 @norcross
11 years ago

My mistake re: trunk, it should have been 3.6.1

#9 in reply to: ↑ 6 @norcross
11 years ago

Replying to nacin:

is_post_type_archive is only set to true when if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {. norcross, you reported this against "trunk" originally. Could anyone actually reproduce this in 3.6? Cause it looks like [25312] broke this. See also #18614 where I suggested a revert.

My mistake re: trunk, it should have been 3.6.1

#10 @SergeyBiryukov
11 years ago

Replying to nacin:

is_post_type_archive is only set to true when if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {.

Right, but the code attached to pre_get_posts (which makes post_type an array) runs after that, not before.

Replying to nacin:

Could anyone actually reproduce this in 3.6?

I was able to reproduce the post-type-archive-Array class in both 3.6 and 3.6.1.

Also got a notice from get_post_type_object(), which appears to be fixed in [25312]:

Warning: Illegal offset type in isset or empty in wp-includes/post.php on line 1038

#11 @nacin
11 years ago

Okay, the direction of this fix is proper. But, we should only add one post type as a body class — the rest of post type archives is limited to the first post type listed, as it is specifically designed to not be for multiple post types. That also has the result of not being a major behavior change, while optionally allowing for multiple body classes later.

#12 @nacin
11 years ago

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

In 25745:

Only add one body class when multiple post types manage to be present for a post type archive due to pre_get_posts manipulation.

fixes #25341.

Note: See TracTickets for help on using tickets.