Opened 5 months ago
Closed 5 months ago
#22956 closed defect (bug) (fixed)
Unpredictable template loading when multiple post types are involved
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.5.1 |
| Component: | Post Types | Version: | 3.5 |
| Severity: | normal | Keywords: | has-patch commit fixed-major |
| Cc: |
Description (last modified by scribu)
[21861] sought to improve the template loading behaviour when multiple post types are involved, but I don't think it was the right approach.
1. It creates backward-incompatible behaviour.
Take a query of 'post_type' => array( 'serie', 'post' )
In 3.4, archive.php always ended up being loaded, since it was unlikely for archive-Array.php to exist.
In 3.5, it unexpectedly loads archive-serie.php.
2. The order of the post types becomes important.
For example, 'post_type' => array( 'serie', 'post' ) can potentially load a different template from 'post_type' => array( 'post', 'serie' ).
Possible alternatives:
a) always load archive.php if 'post_type' is an array.
b) choose the template based on get_queried_object().
Attachments (3)
Change History (22)
- Keywords needs-refresh commit added
Not a huge deal for this one, but any possibility of unit tests here?
Marking as needs refresh to use reset().
- Keywords needs-refresh removed
Refreshed patch.
Not a huge deal for this one, but any possibility of unit tests here?
It would be possible if there was a filter on the list of templates: http://core.trac.wordpress.org/attachment/ticket/14310/template_hierarchy.4.diff
- Keywords commit removed
get_query_var() likes to return '' when a query variable doesn't exist, and we like resetting query vars to '' as well. In this case, we'd actually go looking for an archive-.php file, I'm pretty sure.
The array_filter() in 22956.3.diff should take care of that.
- Keywords commit added
The whole patch is ugly, but effective. :-) Nice, thanks.
comment:10
follow-up:
↓ 11
wonderboymusic — 5 months ago
Just want to go on the record for not liking this at all
comment:11
in reply to:
↑ 10
;
follow-up:
↓ 13
nacin — 5 months ago
Replying to wonderboymusic:
Just want to go on the record for not liking this at all
Could you explain what you'd rather see? Considering we're currently trying to load archive-Array.php and archive-.php, the proper thing to do here would be to restore 3.4 behavior. This doesn't at all prevent us from adding items to the template hierarchy in the future, like checking individual post type archives in some order, or consulting a special multiple post type template, or adding in a filter, or something.
comment:12
wonderboymusic — 5 months ago
this is like doing a WP_Query with 'any' and then loading the archive for the post_type of the first item. That choice is arbitrary. You might as well pick a random post from the collection and load that archive. <IMO>archive.php is the correct template</IMO>. archive-Array and archive-.php both result in archive.php being chosen, so why switch to loading archive-you-pick.php?
comment:13
in reply to:
↑ 11
SergeyBiryukov — 5 months ago
Replying to nacin:
Considering we're currently trying to load archive-Array.php and archive-.php, the proper thing to do here would be to restore 3.4 behavior.
archive-Array.php was the 3.4 behavior (which #20867 attempted to fix).
22956.3.diff looks sane to me.
Replying to wonderboymusic:
this is like doing a WP_Query with 'any' and then loading the archive for the post_type of the first item.
This seems to describe the current behaviour, which 22956.3.diff fixes.
comment:14
wonderboymusic — 5 months ago
count( $post_types ) == 1 ... that makes sense, I am going back to my cave now
comment:15
wonderboymusic — 5 months ago
P.S. I think I freaked out from initially seeing the reset() which reminded me of tax_query and get_queried_object() picking the first one every time: http://core.trac.wordpress.org/browser/trunk/wp-includes/query.php#L2987
comment:16
nacin — 5 months ago
In 23249:
comment:17
nacin — 5 months ago
- Keywords fixed-major added
comment:18
nacin — 5 months ago
In 23271:
comment:19
nacin — 5 months ago
- Resolution set to fixed
- Status changed from new to closed

I was the committing developer on [21861]. You're right, that's the wrong approach.
Can we do $post_type = reset( $post_types );? It's a more common pattern for us than list(), which we usually use for more complex situations.