Make WordPress Core


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

Ensure that the post type object is the queried object when a post type has been registered with has_archive => true. Ensure it is not stomped when decorated with tax_query. Adds unit tests.

Props nacin.
Fixes #18614.

File:
1 edited

Legend:

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

    r25287 r25291  
    649649        $this->assertQueryTrue( 'is_404' );
    650650    }
     651
     652    function test_post_type_archive_with_tax_query() {
     653        delete_option( 'rewrite_rules' );
     654
     655        $cpt_name = 'ptawtq';
     656        register_post_type( $cpt_name, array(
     657            'taxonomies' => array( 'post_tag', 'category' ),
     658            'rewrite' => true,
     659            'has_archive' => true,
     660            'public' => true
     661        ) );
     662
     663        $tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) );
     664        $post_id = $this->factory->post->create( array( 'post_type' => $cpt_name ) );
     665        wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
     666
     667        $this->go_to( '/ptawtq/' );
     668        $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
     669        $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
     670
     671        add_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
     672
     673        $this->go_to( '/ptawtq/' );
     674        $this->assertQueryTrue( 'is_post_type_archive', 'is_archive' );
     675        $this->assertEquals( get_queried_object(), get_post_type_object( $cpt_name ) );
     676
     677        remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_with_tax_query' ) );
     678    }
     679
     680    function pre_get_posts_with_tax_query( &$query ) {
     681        $term = get_term_by( 'slug', 'tag-slug', 'post_tag' );
     682        $query->set( 'tax_query', array(
     683            array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $term->term_id )
     684        ) );
     685    }
    651686}
Note: See TracChangeset for help on using the changeset viewer.