Check the value passed to get_post_type_object(). If it's an array, use the first item. get_query_var( 'post_type' ) can be an array if the query has been altered via filters/actions. There are several places in core that pass the query var. Adds unit tests.

In `template-loader.php`, move `is_post_type_archive()` and `is_tax()` directly below `is_home()`.

See #18614, [25291].



git-svn-id: https://develop.svn.wordpress.org/trunk@25292 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2013-09-06 23:38:51 +00:00
parent 48dc7e576b
commit e688f070dc
3 changed files with 38 additions and 2 deletions

View File

@ -1058,6 +1058,9 @@ function get_post_type( $post = null ) {
function get_post_type_object( $post_type ) {
global $wp_post_types;
if ( is_array( $post_type ) )
$post_type = reset( $post_type );
if ( empty($wp_post_types[$post_type]) )
return null;

View File

@ -26,10 +26,10 @@ if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :
$template = false;
if ( is_404() && $template = get_404_template() ) :
elseif ( is_search() && $template = get_search_template() ) :
elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
elseif ( is_front_page() && $template = get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template() ) :
elseif ( is_post_type_archive() && $template = get_post_type_archive_template() ) :
elseif ( is_tax() && $template = get_taxonomy_template() ) :
elseif ( is_attachment() && $template = get_attachment_template() ) :
remove_filter('the_content', 'prepend_attachment');
elseif ( is_single() && $template = get_single_template() ) :

View File

@ -683,4 +683,37 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $term->term_id )
) );
}
function test_post_type_array() {
delete_option( 'rewrite_rules' );
$cpt_name = 'thearray';
register_post_type( $cpt_name, array(
'taxonomies' => array( 'post_tag', 'category' ),
'rewrite' => true,
'has_archive' => true,
'public' => true
) );
$this->factory->post->create( array( 'post_type' => $cpt_name ) );
$this->go_to( "/$cpt_name/" );
$this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
$this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
$this->assertEquals( get_queried_object(), get_post_type_object( array( $cpt_name ) ) );
$this->assertEquals( get_queried_object(), get_post_type_object( array( $cpt_name, 'post' ) ) );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
$this->go_to( "/$cpt_name/" );
$this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
$this->assertEquals( get_queried_object(), get_post_type_object( 'post' ) );
$this->assertEquals( get_queried_object(), get_post_type_object( array( 'post' ) ) );
$this->assertEquals( get_queried_object(), get_post_type_object( array( 'post', $cpt_name ) ) );
remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
}
function pre_get_posts_with_type_array( &$query ) {
$query->set( 'post_type', array( 'post', 'thearray' ) );
}
}