Make WordPress Core

Ticket #20767: 20767.2.diff

File 20767.2.diff, 3.6 KB (added by wonderboymusic, 10 years ago)
  • src/wp-includes/query.php

     
    30573057                $this->queried_object_id = 0;
    30583058
    30593059                if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    3060                         $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
     3060                        if ( $this->is_tag ) {
     3061                                $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
     3062                        } else if ( $this->is_category ) {
     3063                                $term = get_term( $this->get( 'cat' ), 'category' );
     3064                        } else {
     3065                                $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
     3066                                $query = reset( $tax_query_in_and );
    30613067
    3062                         $query = reset( $tax_query_in_and );
     3068                                if ( 'term_id' == $query['field'] )
     3069                                        $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
     3070                                else
     3071                                        $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
     3072                        }
    30633073
    3064                         if ( 'term_id' == $query['field'] )
    3065                                 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
    3066                         elseif ( $query['terms'] )
    3067                                 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
    3068 
    30693074                        if ( ! empty( $term ) && ! is_wp_error( $term ) )  {
    30703075                                $this->queried_object = $term;
    30713076                                $this->queried_object_id = (int) $term->term_id;
    30723077
    3073                                 if ( $this->is_category )
     3078                                if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
    30743079                                        _make_cat_compat( $this->queried_object );
    30753080                        }
    30763081                } elseif ( $this->is_post_type_archive ) {
  • tests/phpunit/tests/query/conditionals.php

     
    716716        function pre_get_posts_with_type_array( &$query ) {
    717717                $query->set( 'post_type', array( 'post', 'thearray' ) );
    718718        }
     719
     720        function test_tax_category_tax_query() {
     721                register_taxonomy( 'testtax', array( 'public' => true ) );
     722
     723                $tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) );
     724                $cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) );
     725                $tax_id = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug' ) );
     726                $post_id = $this->factory->post->create();
     727                wp_set_object_terms( $post_id, $tag_id, 'post_tag' );
     728                wp_set_object_terms( $post_id, $cat_id, 'category' );
     729                wp_set_object_terms( $post_id, $tax_id, 'testtax' );
     730
     731                add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
     732
     733                $this->go_to( "/tag/tag-slug/" );
     734                $this->assertQueryTrue( 'is_tag', 'is_archive' );
     735                $this->assertEquals( get_queried_object(), get_term( $tag_id, 'post_tag' ) );
     736
     737                $this->go_to( "/category/cat-slug/" );
     738                $this->assertQueryTrue( 'is_category', 'is_archive' );
     739                $cat = get_term( $cat_id, 'category' );
     740                _make_cat_compat( $cat );
     741                $this->assertEquals( get_queried_object(), $cat );
     742
     743                $this->go_to( "/tag/tag-slug/?cat=$cat_id" );
     744                $this->assertQueryTrue( 'is_tag', 'is_category', 'is_archive' );
     745                $this->assertEquals( get_queried_object(), get_term( $tag_id, 'post_tag' ) );
     746
     747                remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) );
     748        }
     749
     750        function pre_get_posts_tax_category_tax_query( &$query ) {
     751                $term = get_term_by( 'slug', 'tax-slug', 'testtax' );
     752                $query->set( 'tax_query', array(
     753                        array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $term->term_id )
     754                ) );
     755        }
    719756}