| | 720 | |
| | 721 | function test_tax_category_tax_query() { |
| | 722 | register_taxonomy( 'testtax', 'post', array( 'query_var' => true ) ); |
| | 723 | delete_option( 'rewrite_rules' ); |
| | 724 | |
| | 725 | $tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) ); |
| | 726 | $cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) ); |
| | 727 | $tax_id = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug' ) ); |
| | 728 | $tax_id2 = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug2' ) ); |
| | 729 | $post_id = $this->factory->post->create(); |
| | 730 | wp_set_object_terms( $post_id, $tag_id, 'post_tag' ); |
| | 731 | wp_set_object_terms( $post_id, $cat_id, 'category' ); |
| | 732 | wp_set_object_terms( $post_id, array( $tax_id, $tax_id2 ), 'testtax' ); |
| | 733 | |
| | 734 | $cat = get_term( $cat_id, 'category' ); |
| | 735 | _make_cat_compat( $cat ); |
| | 736 | |
| | 737 | $tax = get_term( $tax_id, 'testtax' ); |
| | 738 | $tag = get_term( $tag_id, 'post_tag' ); |
| | 739 | |
| | 740 | add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); |
| | 741 | |
| | 742 | // tag with tax added |
| | 743 | $this->go_to( "/tag/tag-slug/" ); |
| | 744 | $this->assertQueryTrue( 'is_tag', 'is_archive' ); |
| | 745 | $this->assertNotEmpty( get_query_var( 'tag_id' ) ); |
| | 746 | $this->assertEquals( get_queried_object(), $tag ); |
| | 747 | |
| | 748 | // tag + category with tax added |
| | 749 | $this->go_to( "/tag/tag-slug/?cat=$cat_id" ); |
| | 750 | $this->assertQueryTrue( 'is_category', 'is_tag', 'is_archive' ); |
| | 751 | $this->assertNotEmpty( get_query_var( 'cat' ) ); |
| | 752 | $this->assertNotEmpty( get_query_var( 'tag_id' ) ); |
| | 753 | $this->assertEquals( get_queried_object(), $cat ); |
| | 754 | |
| | 755 | // tag + category + tax with tax added |
| | 756 | $this->go_to( "/tag/tag-slug/?cat=$cat_id&testtax=tax-slug2" ); |
| | 757 | $this->assertQueryTrue( 'is_category', 'is_tag', 'is_archive' ); |
| | 758 | $this->assertNotEmpty( get_query_var( 'cat' ) ); |
| | 759 | $this->assertNotEmpty( get_query_var( 'tag_id' ) ); |
| | 760 | $this->assertEquals( get_queried_object(), $cat ); |
| | 761 | |
| | 762 | // category with tax added |
| | 763 | $this->go_to( "/category/cat-slug/" ); |
| | 764 | $this->assertQueryTrue( 'is_category', 'is_archive' ); |
| | 765 | $this->assertNotEmpty( get_query_var( 'cat' ) ); |
| | 766 | $this->assertEquals( get_queried_object(), $cat ); |
| | 767 | |
| | 768 | // tag + category with tax added |
| | 769 | $this->go_to( "/tag/tag-slug/?cat=$cat_id" ); |
| | 770 | $this->assertQueryTrue( 'is_tag', 'is_category', 'is_archive' ); |
| | 771 | $this->assertNotEmpty( get_query_var( 'tag_id' ) ); |
| | 772 | $this->assertNotEmpty( get_query_var( 'cat' ) ); |
| | 773 | $this->assertEquals( get_queried_object(), $cat ); |
| | 774 | |
| | 775 | // tax + tag with tax added |
| | 776 | $this->go_to( "/testtax/tax-slug2/?tag_id=$tag_id" ); |
| | 777 | $this->assertQueryTrue( 'is_tag', 'is_archive' ); |
| | 778 | $this->assertNotEmpty( get_query_var( 'tag_id' ) ); |
| | 779 | $this->assertEquals( get_queried_object(), $tag ); |
| | 780 | |
| | 781 | // tax + cat with tax added |
| | 782 | $this->go_to( "/testtax/tax-slug2/?cat=$cat_id" ); |
| | 783 | $this->assertQueryTrue( 'is_category', 'is_archive' ); |
| | 784 | $this->assertNotEmpty( get_query_var( 'cat' ) ); |
| | 785 | $this->assertEquals( get_queried_object(), $cat ); |
| | 786 | |
| | 787 | remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); |
| | 788 | } |
| | 789 | |
| | 790 | function pre_get_posts_tax_category_tax_query( &$query ) { |
| | 791 | $term = get_term_by( 'slug', 'tax-slug', 'testtax' ); |
| | 792 | $query->set( 'tax_query', array( |
| | 793 | array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $term->term_id ) |
| | 794 | ) ); |
| | 795 | } |