Make WordPress Core


Ignore:
Timestamp:
09/06/2013 11:38:51 PM (11 years ago)
Author:
wonderboymusic
Message:

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].

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/conditionals.php

    r25291 r25292  
    684684        ) );
    685685    }
     686
     687    function test_post_type_array() {
     688        delete_option( 'rewrite_rules' );
     689
     690        $cpt_name = 'thearray';
     691        register_post_type( $cpt_name, array(
     692            'taxonomies' => array( 'post_tag', 'category' ),
     693            'rewrite' => true,
     694            'has_archive' => true,
     695            'public' => true
     696        ) );
     697        $this->factory->post->create( array( 'post_type' => $cpt_name ) );
     698
     699        $this->go_to( "/$cpt_name/" );
     700        $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
     701        $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
     702        $this->assertEquals( get_queried_object(), get_post_type_object( array( $cpt_name ) ) );
     703        $this->assertEquals( get_queried_object(), get_post_type_object( array( $cpt_name, 'post' ) ) );
     704
     705        add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
     706
     707        $this->go_to( "/$cpt_name/" );
     708        $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
     709        $this->assertEquals( get_queried_object(), get_post_type_object( 'post' ) );
     710        $this->assertEquals( get_queried_object(), get_post_type_object( array( 'post' ) ) );
     711        $this->assertEquals( get_queried_object(), get_post_type_object( array( 'post', $cpt_name ) ) );
     712
     713        remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_type_array' ) );
     714    }
     715
     716    function pre_get_posts_with_type_array( &$query ) {
     717        $query->set( 'post_type', array( 'post', 'thearray' ) );
     718    }
    686719}
Note: See TracChangeset for help on using the changeset viewer.