Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#42764 closed defect (bug) (wontfix)

Template Hierarchy does not work with multiple post types

Reported by: nbdamian's profile nbdamian Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.6
Component: Posts, Post Types Keywords: close
Focuses: template Cc:

Description

Inside the "pre_get_posts" action, If you set the post_type argument to multiple, I.E:

<?php
add_action( 'pre_get_posts', function($query) {
  if (!is_admin() && $query->is_main_query() && !$query->is_search() && is_post_type_archive('event')):
    $query->set('post_type', array( 'event', 'product' ) );
  endif;
}

This will force the templating hierarchy to use "archive.php" instead of either "archive-event.php" or "archive-product.php"

Currently, I am working around this issue using:

<?php
add_filter( "archive_template_hierarchy", function($templates) {
        $post_types = get_query_var('post_type');
        if (count($post_types) > 1 && count($templates) == 1):
                foreach ($post_types AS $post_type):
                        array_unshift($templates, "archive-".$post_type.".php");
                endforeach;
        endif;
        return $templates;
}, 10, 1 );

Unsure if this is a bug, or intended. Either way, it would be useful to support this.

Change History (4)

#1 in reply to: ↑ description @nbdamian
5 years ago

Replying to nbdamian:

Inside the "pre_get_posts" action, If you set the post_type argument to multiple, I.E:

<?php
add_action( 'pre_get_posts', function($query) {
  if (!is_admin() && $query->is_main_query() && !$query->is_search() && is_post_type_archive('event')):
    $query->set('post_type', array( 'event', 'product' ) );
  endif;
}

This will force the templating hierarchy to use "archive.php" instead of either "archive-event.php" or "archive-product.php"

Currently, I am working around this issue using:

<?php
add_filter( "archive_template_hierarchy", function($templates) {
        $post_types = get_query_var('post_type');
        if (count($post_types) > 1 && count($templates) == 1):
                foreach ($post_types AS $post_type):
                        array_unshift($templates, "archive-".$post_type.".php");
                endforeach;
        endif;
        return $templates;
}, 10, 1 );

Unsure if this is a bug, or intended. Either way, it would be useful to support this.

This could be resolved by changing template.php line 114 from

<?php
if ( count( $post_types ) == 1 ) {
        $post_type = reset( $post_types );
        $templates[] = "archive-{$post_type}.php";
}

To:

<?php
if ( count( $post_types ) == 1 ) {
        $post_type = reset( $post_types );
        $templates[] = "archive-{$post_type}.php";
} elseif ( count( $post_types ) > 1 ) {
        foreach($post_types AS $post_type) {
                $templates[] = "archive-{$post_type}.php";
        }
}

#2 @birgire
5 years ago

  • Component changed from Themes to Posts, Post Types
  • Keywords close added
  • Version changed from 4.9.1 to 3.6

Welcome to trac @nbdamian

This feature was actually removed in 3.6 to avoid unpredictable template loading.

See ticket #22956 and the changeset [23271].

#3 @johnbillion
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

Thanks for the report, @nbdamian , but as @birgire points out above this is intentional behaviour.

#4 @nbdamian
5 years ago

That's fine. Just want to confirm the workaround I am using is the best way to allow what I need?

Note: See TracTickets for help on using tickets.