| 546 | | function test_taxonomy_include_children() { |
| | 515 | /** |
| | 516 | * @group taxonomy |
| | 517 | */ |
| | 518 | public function test_tax_query_single_query_single_term_field_slug() { |
| | 519 | $t = $this->factory->term->create( array( |
| | 520 | 'taxonomy' => 'category', |
| | 521 | 'slug' => 'foo', |
| | 522 | 'name' => 'Foo', |
| | 523 | ) ); |
| | 524 | $p1 = $this->factory->post->create(); |
| | 525 | $p2 = $this->factory->post->create(); |
| | 526 | |
| | 527 | wp_set_post_terms( $p1, $t, 'category' ); |
| | 528 | |
| | 529 | $q = new WP_Query( array( |
| | 530 | 'fields' => 'ids', |
| | 531 | 'update_post_meta_cache' => false, |
| | 532 | 'update_post_term_cache' => false, |
| | 533 | 'tax_query' => array( |
| | 534 | array( |
| | 535 | 'taxonomy' => 'category', |
| | 536 | 'terms' => array( 'foo' ), |
| | 537 | 'field' => 'slug', |
| | 538 | ), |
| | 539 | ), |
| | 540 | ) ); |
| | 541 | |
| | 542 | $this->assertEquals( array( $p1 ), $q->posts ); |
| | 543 | } |
| | 544 | |
| | 545 | /** |
| | 546 | * @group taxonomy |
| | 547 | */ |
| | 548 | public function test_tax_query_single_query_single_term_field_name() { |
| | 549 | $t = $this->factory->term->create( array( |
| | 550 | 'taxonomy' => 'category', |
| | 551 | 'slug' => 'foo', |
| | 552 | 'name' => 'Foo', |
| | 553 | ) ); |
| | 554 | $p1 = $this->factory->post->create(); |
| | 555 | $p2 = $this->factory->post->create(); |
| | 556 | |
| | 557 | wp_set_post_terms( $p1, $t, 'category' ); |
| | 558 | |
| | 559 | $q = new WP_Query( array( |
| | 560 | 'fields' => 'ids', |
| | 561 | 'update_post_meta_cache' => false, |
| | 562 | 'update_post_term_cache' => false, |
| | 563 | 'tax_query' => array( |
| | 564 | array( |
| | 565 | 'taxonomy' => 'category', |
| | 566 | 'terms' => array( 'Foo' ), |
| | 567 | 'field' => 'name', |
| | 568 | ), |
| | 569 | ), |
| | 570 | ) ); |
| | 571 | |
| | 572 | $this->assertEquals( array( $p1 ), $q->posts ); |
| | 573 | } |
| | 574 | |
| | 575 | /** |
| | 576 | * @group taxonomy |
| | 577 | */ |
| | 578 | public function test_tax_query_single_query_single_term_field_term_taxonomy_id() { |
| | 579 | $t = $this->factory->term->create( array( |
| | 580 | 'taxonomy' => 'category', |
| | 581 | 'slug' => 'foo', |
| | 582 | 'name' => 'Foo', |
| | 583 | ) ); |
| | 584 | $p1 = $this->factory->post->create(); |
| | 585 | $p2 = $this->factory->post->create(); |
| | 586 | |
| | 587 | $tt_ids = wp_set_post_terms( $p1, $t, 'category' ); |
| | 588 | |
| | 589 | $q = new WP_Query( array( |
| | 590 | 'fields' => 'ids', |
| | 591 | 'update_post_meta_cache' => false, |
| | 592 | 'update_post_term_cache' => false, |
| | 593 | 'tax_query' => array( |
| | 594 | array( |
| | 595 | 'taxonomy' => 'category', |
| | 596 | 'terms' => $tt_ids, |
| | 597 | 'field' => 'term_taxonomy_id', |
| | 598 | ), |
| | 599 | ), |
| | 600 | ) ); |
| | 601 | |
| | 602 | $this->assertEquals( array( $p1 ), $q->posts ); |
| | 603 | } |
| | 604 | |
| | 605 | /** |
| | 606 | * @group taxonomy |
| | 607 | */ |
| | 608 | public function test_tax_query_single_query_single_term_field_term_id() { |
| | 609 | $t = $this->factory->term->create( array( |
| | 610 | 'taxonomy' => 'category', |
| | 611 | 'slug' => 'foo', |
| | 612 | 'name' => 'Foo', |
| | 613 | ) ); |
| | 614 | $p1 = $this->factory->post->create(); |
| | 615 | $p2 = $this->factory->post->create(); |
| | 616 | |
| | 617 | wp_set_post_terms( $p1, $t, 'category' ); |
| | 618 | |
| | 619 | $q = new WP_Query( array( |
| | 620 | 'fields' => 'ids', |
| | 621 | 'update_post_meta_cache' => false, |
| | 622 | 'update_post_term_cache' => false, |
| | 623 | 'tax_query' => array( |
| | 624 | array( |
| | 625 | 'taxonomy' => 'category', |
| | 626 | 'terms' => array( $t ), |
| | 627 | 'field' => 'term_id', |
| | 628 | ), |
| | 629 | ), |
| | 630 | ) ); |
| | 631 | |
| | 632 | $this->assertEquals( array( $p1 ), $q->posts ); |
| | 633 | } |
| | 634 | |
| | 635 | /** |
| | 636 | * @group taxonomy |
| | 637 | */ |
| | 638 | public function test_tax_query_single_query_single_term_operator_in() { |
| | 639 | $t = $this->factory->term->create( array( |
| | 640 | 'taxonomy' => 'category', |
| | 641 | 'slug' => 'foo', |
| | 642 | 'name' => 'Foo', |
| | 643 | ) ); |
| | 644 | $p1 = $this->factory->post->create(); |
| | 645 | $p2 = $this->factory->post->create(); |
| | 646 | |
| | 647 | wp_set_post_terms( $p1, $t, 'category' ); |
| | 648 | |
| | 649 | $q = new WP_Query( array( |
| | 650 | 'fields' => 'ids', |
| | 651 | 'update_post_meta_cache' => false, |
| | 652 | 'update_post_term_cache' => false, |
| | 653 | 'tax_query' => array( |
| | 654 | array( |
| | 655 | 'taxonomy' => 'category', |
| | 656 | 'terms' => array( 'foo' ), |
| | 657 | 'field' => 'slug', |
| | 658 | 'operator' => 'IN', |
| | 659 | ), |
| | 660 | ), |
| | 661 | ) ); |
| | 662 | |
| | 663 | $this->assertEquals( array( $p1 ), $q->posts ); |
| | 664 | } |
| | 665 | |
| | 666 | /** |
| | 667 | * @group taxonomy |
| | 668 | */ |
| | 669 | public function test_tax_query_single_query_single_term_operator_not_in() { |
| | 670 | $t = $this->factory->term->create( array( |
| | 671 | 'taxonomy' => 'category', |
| | 672 | 'slug' => 'foo', |
| | 673 | 'name' => 'Foo', |
| | 674 | ) ); |
| | 675 | $p1 = $this->factory->post->create(); |
| | 676 | $p2 = $this->factory->post->create(); |
| | 677 | |
| | 678 | wp_set_post_terms( $p1, $t, 'category' ); |
| | 679 | |
| | 680 | $q = new WP_Query( array( |
| | 681 | 'fields' => 'ids', |
| | 682 | 'update_post_meta_cache' => false, |
| | 683 | 'update_post_term_cache' => false, |
| | 684 | 'tax_query' => array( |
| | 685 | array( |
| | 686 | 'taxonomy' => 'category', |
| | 687 | 'terms' => array( 'foo' ), |
| | 688 | 'field' => 'slug', |
| | 689 | 'operator' => 'NOT IN', |
| | 690 | ), |
| | 691 | ), |
| | 692 | ) ); |
| | 693 | |
| | 694 | $this->assertEquals( array( $p2 ), $q->posts ); |
| | 695 | } |
| | 696 | |
| | 697 | /** |
| | 698 | * @group taxonomy |
| | 699 | */ |
| | 700 | public function test_tax_query_single_query_single_term_operator_and() { |
| | 701 | $t = $this->factory->term->create( array( |
| | 702 | 'taxonomy' => 'category', |
| | 703 | 'slug' => 'foo', |
| | 704 | 'name' => 'Foo', |
| | 705 | ) ); |
| | 706 | $p1 = $this->factory->post->create(); |
| | 707 | $p2 = $this->factory->post->create(); |
| | 708 | |
| | 709 | wp_set_post_terms( $p1, $t, 'category' ); |
| | 710 | |
| | 711 | $q = new WP_Query( array( |
| | 712 | 'fields' => 'ids', |
| | 713 | 'update_post_meta_cache' => false, |
| | 714 | 'update_post_term_cache' => false, |
| | 715 | 'tax_query' => array( |
| | 716 | array( |
| | 717 | 'taxonomy' => 'category', |
| | 718 | 'terms' => array( 'foo' ), |
| | 719 | 'field' => 'slug', |
| | 720 | 'operator' => 'AND', |
| | 721 | ), |
| | 722 | ), |
| | 723 | ) ); |
| | 724 | |
| | 725 | $this->assertEquals( array( $p1 ), $q->posts ); |
| | 726 | } |
| | 727 | |
| | 728 | /** |
| | 729 | * @group taxonomy |
| | 730 | */ |
| | 731 | public function test_tax_query_single_query_multiple_terms_operator_in() { |
| | 732 | $t1 = $this->factory->term->create( array( |
| | 733 | 'taxonomy' => 'category', |
| | 734 | 'slug' => 'foo', |
| | 735 | 'name' => 'Foo', |
| | 736 | ) ); |
| | 737 | $t2 = $this->factory->term->create( array( |
| | 738 | 'taxonomy' => 'category', |
| | 739 | 'slug' => 'bar', |
| | 740 | 'name' => 'Bar', |
| | 741 | ) ); |
| | 742 | $p1 = $this->factory->post->create(); |
| | 743 | $p2 = $this->factory->post->create(); |
| | 744 | $p3 = $this->factory->post->create(); |
| | 745 | |
| | 746 | wp_set_post_terms( $p1, $t1, 'category' ); |
| | 747 | wp_set_post_terms( $p2, $t2, 'category' ); |
| | 748 | |
| | 749 | $q = new WP_Query( array( |
| | 750 | 'fields' => 'ids', |
| | 751 | 'update_post_meta_cache' => false, |
| | 752 | 'update_post_term_cache' => false, |
| | 753 | 'tax_query' => array( |
| | 754 | array( |
| | 755 | 'taxonomy' => 'category', |
| | 756 | 'terms' => array( 'foo', 'bar' ), |
| | 757 | 'field' => 'slug', |
| | 758 | 'operator' => 'IN', |
| | 759 | ), |
| | 760 | ), |
| | 761 | ) ); |
| | 762 | |
| | 763 | $this->assertEquals( array( $p1, $p2 ), $q->posts ); |
| | 764 | } |
| | 765 | |
| | 766 | /** |
| | 767 | * @group taxonomy |
| | 768 | */ |
| | 769 | public function test_tax_query_single_query_multiple_terms_operator_not_in() { |
| | 770 | $t1 = $this->factory->term->create( array( |
| | 771 | 'taxonomy' => 'category', |
| | 772 | 'slug' => 'foo', |
| | 773 | 'name' => 'Foo', |
| | 774 | ) ); |
| | 775 | $t2 = $this->factory->term->create( array( |
| | 776 | 'taxonomy' => 'category', |
| | 777 | 'slug' => 'bar', |
| | 778 | 'name' => 'Bar', |
| | 779 | ) ); |
| | 780 | $p1 = $this->factory->post->create(); |
| | 781 | $p2 = $this->factory->post->create(); |
| | 782 | $p3 = $this->factory->post->create(); |
| | 783 | |
| | 784 | wp_set_post_terms( $p1, $t1, 'category' ); |
| | 785 | wp_set_post_terms( $p2, $t2, 'category' ); |
| | 786 | |
| | 787 | $q = new WP_Query( array( |
| | 788 | 'fields' => 'ids', |
| | 789 | 'update_post_meta_cache' => false, |
| | 790 | 'update_post_term_cache' => false, |
| | 791 | 'tax_query' => array( |
| | 792 | array( |
| | 793 | 'taxonomy' => 'category', |
| | 794 | 'terms' => array( 'foo', 'bar' ), |
| | 795 | 'field' => 'slug', |
| | 796 | 'operator' => 'NOT IN', |
| | 797 | ), |
| | 798 | ), |
| | 799 | ) ); |
| | 800 | |
| | 801 | $this->assertEquals( array( $p3 ), $q->posts ); |
| | 802 | } |
| | 803 | |
| | 804 | /** |
| | 805 | * @group taxonomy |
| | 806 | */ |
| | 807 | public function test_tax_query_single_query_multiple_terms_operator_and() { |
| | 808 | $t1 = $this->factory->term->create( array( |
| | 809 | 'taxonomy' => 'category', |
| | 810 | 'slug' => 'foo', |
| | 811 | 'name' => 'Foo', |
| | 812 | ) ); |
| | 813 | $t2 = $this->factory->term->create( array( |
| | 814 | 'taxonomy' => 'category', |
| | 815 | 'slug' => 'bar', |
| | 816 | 'name' => 'Bar', |
| | 817 | ) ); |
| | 818 | $p1 = $this->factory->post->create(); |
| | 819 | $p2 = $this->factory->post->create(); |
| | 820 | $p3 = $this->factory->post->create(); |
| | 821 | |
| | 822 | wp_set_object_terms( $p1, $t1, 'category' ); |
| | 823 | wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); |
| | 824 | |
| | 825 | $q = new WP_Query( array( |
| | 826 | 'fields' => 'ids', |
| | 827 | 'update_post_meta_cache' => false, |
| | 828 | 'update_post_term_cache' => false, |
| | 829 | 'tax_query' => array( |
| | 830 | array( |
| | 831 | 'taxonomy' => 'category', |
| | 832 | 'terms' => array( 'foo', 'bar' ), |
| | 833 | 'field' => 'slug', |
| | 834 | 'operator' => 'AND', |
| | 835 | ), |
| | 836 | ), |
| | 837 | ) ); |
| | 838 | |
| | 839 | $this->assertEquals( array( $p2 ), $q->posts ); |
| | 840 | } |
| | 841 | |
| | 842 | /** |
| | 843 | * @group taxonomy |
| | 844 | */ |
| | 845 | public function test_tax_query_multiple_queries_relation_and() { |
| | 846 | $t1 = $this->factory->term->create( array( |
| | 847 | 'taxonomy' => 'category', |
| | 848 | 'slug' => 'foo', |
| | 849 | 'name' => 'Foo', |
| | 850 | ) ); |
| | 851 | $t2 = $this->factory->term->create( array( |
| | 852 | 'taxonomy' => 'category', |
| | 853 | 'slug' => 'bar', |
| | 854 | 'name' => 'Bar', |
| | 855 | ) ); |
| | 856 | $p1 = $this->factory->post->create(); |
| | 857 | $p2 = $this->factory->post->create(); |
| | 858 | $p3 = $this->factory->post->create(); |
| | 859 | |
| | 860 | wp_set_object_terms( $p1, $t1, 'category' ); |
| | 861 | wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); |
| | 862 | |
| | 863 | $q = new WP_Query( array( |
| | 864 | 'fields' => 'ids', |
| | 865 | 'update_post_meta_cache' => false, |
| | 866 | 'update_post_term_cache' => false, |
| | 867 | 'tax_query' => array( |
| | 868 | 'relation' => 'AND', |
| | 869 | array( |
| | 870 | 'taxonomy' => 'category', |
| | 871 | 'terms' => array( 'foo' ), |
| | 872 | 'field' => 'slug', |
| | 873 | ), |
| | 874 | array( |
| | 875 | 'taxonomy' => 'category', |
| | 876 | 'terms' => array( 'bar' ), |
| | 877 | 'field' => 'slug', |
| | 878 | ), |
| | 879 | ), |
| | 880 | ) ); |
| | 881 | |
| | 882 | $this->assertEquals( array( $p2 ), $q->posts ); |
| | 883 | } |
| | 884 | |
| | 885 | /** |
| | 886 | * @group taxonomy |
| | 887 | */ |
| | 888 | public function test_tax_query_multiple_queries_relation_or() { |
| | 889 | $t1 = $this->factory->term->create( array( |
| | 890 | 'taxonomy' => 'category', |
| | 891 | 'slug' => 'foo', |
| | 892 | 'name' => 'Foo', |
| | 893 | ) ); |
| | 894 | $t2 = $this->factory->term->create( array( |
| | 895 | 'taxonomy' => 'category', |
| | 896 | 'slug' => 'bar', |
| | 897 | 'name' => 'Bar', |
| | 898 | ) ); |
| | 899 | $p1 = $this->factory->post->create(); |
| | 900 | $p2 = $this->factory->post->create(); |
| | 901 | $p3 = $this->factory->post->create(); |
| | 902 | |
| | 903 | wp_set_object_terms( $p1, $t1, 'category' ); |
| | 904 | wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); |
| | 905 | |
| | 906 | $q = new WP_Query( array( |
| | 907 | 'fields' => 'ids', |
| | 908 | 'update_post_meta_cache' => false, |
| | 909 | 'update_post_term_cache' => false, |
| | 910 | 'tax_query' => array( |
| | 911 | 'relation' => 'OR', |
| | 912 | array( |
| | 913 | 'taxonomy' => 'category', |
| | 914 | 'terms' => array( 'foo' ), |
| | 915 | 'field' => 'slug', |
| | 916 | ), |
| | 917 | array( |
| | 918 | 'taxonomy' => 'category', |
| | 919 | 'terms' => array( 'bar' ), |
| | 920 | 'field' => 'slug', |
| | 921 | ), |
| | 922 | ), |
| | 923 | ) ); |
| | 924 | |
| | 925 | $this->assertEquals( array( $p1, $p2 ), $q->posts ); |
| | 926 | } |
| | 927 | |
| | 928 | /** |
| | 929 | * @group taxonomy |
| | 930 | */ |
| | 931 | public function test_tax_query_multiple_queries_different_taxonomies() { |
| | 932 | $t1 = $this->factory->term->create( array( |
| | 933 | 'taxonomy' => 'post_tag', |
| | 934 | 'slug' => 'foo', |
| | 935 | 'name' => 'Foo', |
| | 936 | ) ); |
| | 937 | $t2 = $this->factory->term->create( array( |
| | 938 | 'taxonomy' => 'category', |
| | 939 | 'slug' => 'bar', |
| | 940 | 'name' => 'Bar', |
| | 941 | ) ); |
| | 942 | $p1 = $this->factory->post->create(); |
| | 943 | $p2 = $this->factory->post->create(); |
| | 944 | $p3 = $this->factory->post->create(); |
| | 945 | |
| | 946 | wp_set_object_terms( $p1, $t1, 'post_tag' ); |
| | 947 | wp_set_object_terms( $p2, $t2, 'category' ); |
| | 948 | |
| | 949 | $q = new WP_Query( array( |
| | 950 | 'fields' => 'ids', |
| | 951 | 'update_post_meta_cache' => false, |
| | 952 | 'update_post_term_cache' => false, |
| | 953 | 'tax_query' => array( |
| | 954 | 'relation' => 'OR', |
| | 955 | array( |
| | 956 | 'taxonomy' => 'post_tag', |
| | 957 | 'terms' => array( 'foo' ), |
| | 958 | 'field' => 'slug', |
| | 959 | ), |
| | 960 | array( |
| | 961 | 'taxonomy' => 'category', |
| | 962 | 'terms' => array( 'bar' ), |
| | 963 | 'field' => 'slug', |
| | 964 | ), |
| | 965 | ), |
| | 966 | ) ); |
| | 967 | |
| | 968 | $this->assertEquals( array( $p1, $p2 ), $q->posts ); |
| | 969 | } |
| | 970 | |
| | 971 | /** |
| | 972 | * @ticket 20604 |
| | 973 | * @group taxonomy |
| | 974 | */ |
| | 975 | public function test_tax_query_relation_or_both_clauses_empty_terms() { |
| | 976 | // An empty tax query should return an empty array, not all posts. |
| | 977 | |
| | 978 | $this->factory->post->create_many( 10 ); |
| | 979 | |
| | 980 | $query = new WP_Query( array( |
| | 981 | 'fields' => 'ids', |
| | 982 | 'update_post_term_cache' => false, |
| | 983 | 'update_post_meta_cache' => false, |
| | 984 | 'tax_query' => array( |
| | 985 | 'relation' => 'OR', |
| | 986 | array( |
| | 987 | 'taxonomy' => 'post_tag', |
| | 988 | 'field' => 'id', |
| | 989 | 'terms' => false, |
| | 990 | 'operator' => 'IN' |
| | 991 | ), |
| | 992 | array( |
| | 993 | 'taxonomy' => 'category', |
| | 994 | 'field' => 'id', |
| | 995 | 'terms' => false, |
| | 996 | 'operator' => 'IN' |
| | 997 | ), |
| | 998 | ) |
| | 999 | ) ); |
| | 1000 | |
| | 1001 | $posts = $query->get_posts(); |
| | 1002 | $this->assertEquals( 0 , count( $posts ) ); |
| | 1003 | } |
| | 1004 | |
| | 1005 | /** |
| | 1006 | * @ticket 20604 |
| | 1007 | * @group taxonomy |
| | 1008 | */ |
| | 1009 | public function test_tax_query_relation_or_one_clause_empty_terms() { |
| | 1010 | // An empty tax query should return an empty array, not all posts. |
| | 1011 | |
| | 1012 | $this->factory->post->create_many( 10 ); |
| | 1013 | |
| | 1014 | $query = new WP_Query( array( |
| | 1015 | 'fields' => 'ids', |
| | 1016 | 'update_post_term_cache' => false, |
| | 1017 | 'update_post_meta_cache' => false, |
| | 1018 | 'tax_query' => array( |
| | 1019 | 'relation' => 'OR', |
| | 1020 | array( |
| | 1021 | 'taxonomy' => 'post_tag', |
| | 1022 | 'field' => 'id', |
| | 1023 | 'terms' => array( 'foo' ), |
| | 1024 | 'operator' => 'IN' |
| | 1025 | ), |
| | 1026 | array( |
| | 1027 | 'taxonomy' => 'category', |
| | 1028 | 'field' => 'id', |
| | 1029 | 'terms' => false, |
| | 1030 | 'operator' => 'IN' |
| | 1031 | ), |
| | 1032 | ) |
| | 1033 | ) ); |
| | 1034 | |
| | 1035 | $posts = $query->get_posts(); |
| | 1036 | $this->assertEquals( 0 , count( $posts ) ); |
| | 1037 | } |
| | 1038 | |
| | 1039 | /** |
| | 1040 | * @group taxonomy |
| | 1041 | */ |
| | 1042 | public function test_tax_query_include_children() { |
| | 1148 | * @group taxonomy |
| | 1149 | */ |
| | 1150 | function test_category__and_var() { |
| | 1151 | $q = new WP_Query(); |
| | 1152 | |
| | 1153 | $term_id = $this->factory->category->create( array( 'slug' => 'woo', 'name' => 'WOO!' ) ); |
| | 1154 | $term_id2 = $this->factory->category->create( array( 'slug' => 'hoo', 'name' => 'HOO!' ) ); |
| | 1155 | $post_id = $this->factory->post->create(); |
| | 1156 | |
| | 1157 | wp_set_post_categories( $post_id, $term_id ); |
| | 1158 | |
| | 1159 | $posts = $q->query( array( 'category__and' => array( $term_id ) ) ); |
| | 1160 | |
| | 1161 | $this->assertEmpty( $q->get( 'category__and' ) ); |
| | 1162 | $this->assertCount( 0, $q->get( 'category__and' ) ); |
| | 1163 | $this->assertNotEmpty( $q->get( 'category__in' ) ); |
| | 1164 | $this->assertCount( 1, $q->get( 'category__in' ) ); |
| | 1165 | |
| | 1166 | $this->assertNotEmpty( $posts ); |
| | 1167 | $this->assertEquals( array( $post_id ), wp_list_pluck( $posts, 'ID' ) ); |
| | 1168 | |
| | 1169 | $posts2 = $q->query( array( 'category__and' => array( $term_id, $term_id2 ) ) ); |
| | 1170 | $this->assertNotEmpty( $q->get( 'category__and' ) ); |
| | 1171 | $this->assertCount( 2, $q->get( 'category__and' ) ); |
| | 1172 | $this->assertEmpty( $q->get( 'category__in' ) ); |
| | 1173 | $this->assertCount( 0, $q->get( 'category__in' ) ); |
| | 1174 | |
| | 1175 | $this->assertEmpty( $posts2 ); |
| | 1176 | } |
| | 1177 | |
| | 1178 | /** |
| | 1179 | * @group taxonomy |
| | 1180 | */ |
| | 1181 | public function test_tax_query_taxonomy_with_attachments() { |
| | 1182 | $q = new WP_Query(); |
| | 1183 | |
| | 1184 | register_taxonomy_for_object_type( 'post_tag', 'attachment:image' ); |
| | 1185 | $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) ); |
| | 1186 | $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array( |
| | 1187 | 'post_mime_type' => 'image/jpeg', |
| | 1188 | 'post_type' => 'attachment' |
| | 1189 | ) ); |
| | 1190 | wp_set_object_terms( $image_id, $tag_id, 'post_tag' ); |
| | 1191 | |
| | 1192 | $posts = $q->query( array( |
| | 1193 | 'fields' => 'ids', |
| | 1194 | 'update_post_meta_cache' => false, |
| | 1195 | 'update_post_term_cache' => false, |
| | 1196 | 'post_type' => 'attachment', |
| | 1197 | 'post_status' => 'inherit', |
| | 1198 | 'tax_query' => array( |
| | 1199 | array( |
| | 1200 | 'taxonomy' => 'post_tag', |
| | 1201 | 'field' => 'term_id', |
| | 1202 | 'terms' => array( $tag_id ) |
| | 1203 | ) |
| | 1204 | ) |
| | 1205 | ) ); |
| | 1206 | |
| | 1207 | $this->assertEquals( array( $image_id ), $posts ); |
| | 1208 | } |
| | 1209 | |
| | 1210 | /** |
| | 1211 | * @ticket 27193 |
| | 1212 | * @group taxonomy |
| | 1213 | */ |
| | 1214 | function test_cat_or_tag() { |
| | 1215 | $category1 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'alpha' ) ); |
| | 1216 | $category2 = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'beta' ) ); |
| | 1217 | |
| | 1218 | $tag1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'gamma' ) ); |
| | 1219 | $tag2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'delta' ) ); |
| | 1220 | |
| | 1221 | $post_id1 = $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $category1 ) ) ); |
| | 1222 | $terms1 = get_the_category( $post_id1 ); |
| | 1223 | $this->assertEquals( array( get_category( $category1 ) ), $terms1 ); |
| | 1224 | |
| | 1225 | $post_id2 = $this->factory->post->create( array( 'post_title' => 'beta', 'post_category' => array( $category2 ) ) ); |
| | 1226 | $terms2 = get_the_category( $post_id2 ); |
| | 1227 | $this->assertEquals( array( get_category( $category2 ) ), $terms2 ); |
| | 1228 | |
| | 1229 | $post_id3 = $this->factory->post->create( array( 'post_title' => 'gamma', 'post_tag' => array( $tag1 ) ) ); |
| | 1230 | $post_id4 = $this->factory->post->create( array( 'post_title' => 'delta', 'post_tag' => array( $tag2 ) ) ); |
| | 1231 | |
| | 1232 | $query = new WP_Query( array( |
| | 1233 | 'fields' => 'ids', |
| | 1234 | 'update_post_meta_cache' => false, |
| | 1235 | 'update_post_term_cache' => false, |
| | 1236 | 'tax_query' => array( |
| | 1237 | //'relation' => 'OR', |
| | 1238 | array( |
| | 1239 | 'taxonomy' => 'category', |
| | 1240 | 'field' => 'term_id', |
| | 1241 | 'terms' => array( $category1, $category2 ) |
| | 1242 | ) |
| | 1243 | ) |
| | 1244 | ) ); |
| | 1245 | $ids = $query->get_posts(); |
| | 1246 | $this->assertEquals( array( $post_id1, $post_id2 ), $ids ); |
| | 1247 | } |
| | 1248 | |
| | 1249 | /** |
| | 1250 | * @group taxonomy |
| | 1251 | */ |
| | 1252 | function test_tax_query_no_taxonomy() { |
| | 1253 | $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) ); |
| | 1254 | $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) ); |
| | 1255 | |
| | 1256 | $response1 = new WP_Query( array( |
| | 1257 | 'tax_query' => array( |
| | 1258 | array( 'terms' => array( $cat_id ) ) |
| | 1259 | ) |
| | 1260 | ) ); |
| | 1261 | $this->assertEmpty( $response1->posts ); |
| | 1262 | |
| | 1263 | $response2 = new WP_Query( array( |
| | 1264 | 'fields' => 'ids', |
| | 1265 | 'update_post_meta_cache' => false, |
| | 1266 | 'update_post_term_cache' => false, |
| | 1267 | 'tax_query' => array( |
| | 1268 | array( |
| | 1269 | 'taxonomy' => 'category', |
| | 1270 | 'terms' => array( $cat_id ) |
| | 1271 | ) |
| | 1272 | ) |
| | 1273 | ) ); |
| | 1274 | $this->assertNotEmpty( $response2->posts ); |
| | 1275 | |
| | 1276 | $term = get_category( $cat_id ); |
| | 1277 | $response3 = new WP_Query( array( |
| | 1278 | 'fields' => 'ids', |
| | 1279 | 'update_post_meta_cache' => false, |
| | 1280 | 'update_post_term_cache' => false, |
| | 1281 | 'tax_query' => array( |
| | 1282 | array( |
| | 1283 | 'field' => 'term_taxonomy_id', |
| | 1284 | 'terms' => array( $term->term_taxonomy_id ) |
| | 1285 | ) |
| | 1286 | ) |
| | 1287 | ) ); |
| | 1288 | $this->assertNotEmpty( $response3->posts ); |
| | 1289 | } |
| | 1290 | |
| | 1291 | /** |
| | 1292 | * @group taxonomy |
| | 1293 | */ |
| | 1294 | function test_term_taxonomy_id_field_no_taxonomy() { |
| | 1295 | $q = new WP_Query(); |
| | 1296 | |
| | 1297 | $posts = $this->factory->post->create_many( 5 ); |
| | 1298 | |
| | 1299 | $cats = $tags = array(); |
| | 1300 | |
| | 1301 | // need term_taxonomy_ids in addition to term_ids, so no factory |
| | 1302 | for ( $i = 0; $i < 5; $i++ ) { |
| | 1303 | $cats[$i] = wp_insert_term( 'category-' . $i , 'category' ); |
| | 1304 | $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' ); |
| | 1305 | |
| | 1306 | // post 0 gets all terms |
| | 1307 | wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true ); |
| | 1308 | wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true ); |
| | 1309 | } |
| | 1310 | |
| | 1311 | wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); |
| | 1312 | wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' ); |
| | 1313 | |
| | 1314 | wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' ); |
| | 1315 | wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); |
| | 1316 | |
| | 1317 | wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); |
| | 1318 | wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); |
| | 1319 | |
| | 1320 | $results1 = $q->query( array( |
| | 1321 | 'fields' => 'ids', |
| | 1322 | 'update_post_meta_cache' => false, |
| | 1323 | 'update_post_term_cache' => false, |
| | 1324 | 'orderby' => 'ID', |
| | 1325 | 'order' => 'ASC', |
| | 1326 | 'tax_query' => array( |
| | 1327 | 'relation' => 'OR', |
| | 1328 | array( |
| | 1329 | 'field' => 'term_taxonomy_id', |
| | 1330 | 'terms' => array( $cats[0]['term_taxonomy_id'], $cats[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'], $tags[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'] ), |
| | 1331 | 'operator' => 'AND', |
| | 1332 | 'include_children' => false, |
| | 1333 | ), |
| | 1334 | array( |
| | 1335 | 'field' => 'term_taxonomy_id', |
| | 1336 | 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), |
| | 1337 | 'operator' => 'AND', |
| | 1338 | 'include_children' => false, |
| | 1339 | ) |
| | 1340 | ) |
| | 1341 | ) ); |
| | 1342 | |
| | 1343 | $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' ); |
| | 1344 | |
| | 1345 | $results2 = $q->query( array( |
| | 1346 | 'fields' => 'ids', |
| | 1347 | 'update_post_meta_cache' => false, |
| | 1348 | 'update_post_term_cache' => false, |
| | 1349 | 'orderby' => 'ID', |
| | 1350 | 'order' => 'ASC', |
| | 1351 | 'tax_query' => array( |
| | 1352 | 'relation' => 'AND', |
| | 1353 | array( |
| | 1354 | 'field' => 'term_taxonomy_id', |
| | 1355 | 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ), |
| | 1356 | 'operator' => 'IN', |
| | 1357 | 'include_children' => false, |
| | 1358 | ), |
| | 1359 | array( |
| | 1360 | 'field' => 'term_taxonomy_id', |
| | 1361 | 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), |
| | 1362 | 'operator' => 'IN', |
| | 1363 | 'include_children' => false, |
| | 1364 | ) |
| | 1365 | ) |
| | 1366 | ) ); |
| | 1367 | |
| | 1368 | $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' ); |
| | 1369 | } |
| | 1370 | |
| | 1371 | /** |
| | 1372 | * @ticket 28099 |
| | 1373 | * @group taxonomy |
| | 1374 | */ |
| | 1375 | function test_empty_category__in() { |
| | 1376 | $cat_id = $this->factory->category->create(); |
| | 1377 | $post_id = $this->factory->post->create(); |
| | 1378 | wp_set_post_categories( $post_id, $cat_id ); |
| | 1379 | |
| | 1380 | $q1 = get_posts( array( 'category__in' => array( $cat_id ) ) ); |
| | 1381 | $this->assertNotEmpty( $q1 ); |
| | 1382 | $q2 = get_posts( array( 'category__in' => array() ) ); |
| | 1383 | $this->assertNotEmpty( $q2 ); |
| | 1384 | |
| | 1385 | $tag = wp_insert_term( 'woo', 'post_tag' ); |
| | 1386 | $tag_id = $tag['term_id']; |
| | 1387 | $slug = get_tag( $tag_id )->slug; |
| | 1388 | wp_set_post_tags( $post_id, $slug ); |
| | 1389 | |
| | 1390 | $q3 = get_posts( array( 'tag__in' => array( $tag_id ) ) ); |
| | 1391 | $this->assertNotEmpty( $q3 ); |
| | 1392 | $q4 = get_posts( array( 'tag__in' => array() ) ); |
| | 1393 | $this->assertNotEmpty( $q4 ); |
| | 1394 | |
| | 1395 | $q5 = get_posts( array( 'tag_slug__in' => array( $slug ) ) ); |
| | 1396 | $this->assertNotEmpty( $q5 ); |
| | 1397 | $q6 = get_posts( array( 'tag_slug__in' => array() ) ); |
| | 1398 | $this->assertNotEmpty( $q6 ); |
| | 1399 | } |
| | 1400 | |
| | 1401 | /** |
| | 1402 | * @group taxonomy |
| | 1403 | * @ticket 29718 |
| | 1404 | */ |
| | 1405 | public function test_populate_taxonomy_query_var_from_tax_query() { |
| | 1406 | register_taxonomy( 'foo', 'post' ); |
| | 1407 | $t = $this->factory->term->create( array( |
| | 1408 | 'taxonomy' => 'foo', |
| | 1409 | ) ); |
| | 1410 | $c = $this->factory->term->create( array( |
| | 1411 | 'taxonomy' => 'category', |
| | 1412 | ) ); |
| | 1413 | |
| | 1414 | $q = new WP_Query( array( |
| | 1415 | 'tax_query' => array( |
| | 1416 | // Empty terms mean that this one should be skipped |
| | 1417 | array( |
| | 1418 | 'taxonomy' => 'bar', |
| | 1419 | 'terms' => array(), |
| | 1420 | ), |
| | 1421 | |
| | 1422 | // Category and post tags should be skipped |
| | 1423 | array( |
| | 1424 | 'taxonomy' => 'category', |
| | 1425 | 'terms' => array( $c ), |
| | 1426 | ), |
| | 1427 | |
| | 1428 | array( |
| | 1429 | 'taxonomy' => 'foo', |
| | 1430 | 'terms' => array( $t ), |
| | 1431 | ), |
| | 1432 | ), |
| | 1433 | ) ); |
| | 1434 | |
| | 1435 | $this->assertSame( 'foo', $q->get( 'taxonomy' ) ); |
| | 1436 | |
| | 1437 | _unregister_taxonomy( 'foo' ); |
| | 1438 | } |
| | 1439 | |
| | 1440 | /** |
| | 1441 | * @group taxonomy |
| | 1442 | */ |
| | 1443 | public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() { |
| | 1444 | register_taxonomy( 'foo', 'post' ); |
| | 1445 | register_taxonomy( 'foo1', 'post' ); |
| | 1446 | $t = $this->factory->term->create( array( |
| | 1447 | 'taxonomy' => 'foo', |
| | 1448 | ) ); |
| | 1449 | |
| | 1450 | $q = new WP_Query( array( |
| | 1451 | 'taxonomy' => 'bar', |
| | 1452 | 'tax_query' => array( |
| | 1453 | array( |
| | 1454 | 'taxonomy' => 'foo', |
| | 1455 | 'terms' => array( $t ), |
| | 1456 | ), |
| | 1457 | ), |
| | 1458 | ) ); |
| | 1459 | |
| | 1460 | $this->assertSame( 'bar', $q->get( 'taxonomy' ) ); |
| | 1461 | |
| | 1462 | _unregister_taxonomy( 'foo' ); |
| | 1463 | _unregister_taxonomy( 'bar' ); |
| | 1464 | } |
| | 1465 | |
| | 1466 | /** |
| | 1467 | * @group taxonomy |
| | 1468 | */ |
| | 1469 | public function test_populate_term_query_var_from_tax_query() { |
| | 1470 | register_taxonomy( 'foo', 'post' ); |
| | 1471 | $t = $this->factory->term->create( array( |
| | 1472 | 'taxonomy' => 'foo', |
| | 1473 | 'slug' => 'bar', |
| | 1474 | ) ); |
| | 1475 | |
| | 1476 | $q = new WP_Query( array( |
| | 1477 | 'tax_query' => array( |
| | 1478 | array( |
| | 1479 | 'taxonomy' => 'foo', |
| | 1480 | 'terms' => array( 'bar' ), |
| | 1481 | 'field' => 'slug', |
| | 1482 | ), |
| | 1483 | ), |
| | 1484 | ) ); |
| | 1485 | |
| | 1486 | $this->assertSame( 'bar', $q->get( 'term' ) ); |
| | 1487 | |
| | 1488 | _unregister_taxonomy( 'foo' ); |
| | 1489 | } |
| | 1490 | |
| | 1491 | /** |
| | 1492 | * @group taxonomy |
| | 1493 | */ |
| | 1494 | public function test_populate_term_id_query_var_from_tax_query() { |
| | 1495 | register_taxonomy( 'foo', 'post' ); |
| | 1496 | $t = $this->factory->term->create( array( |
| | 1497 | 'taxonomy' => 'foo', |
| | 1498 | 'slug' => 'bar', |
| | 1499 | ) ); |
| | 1500 | |
| | 1501 | $q = new WP_Query( array( |
| | 1502 | 'tax_query' => array( |
| | 1503 | array( |
| | 1504 | 'taxonomy' => 'foo', |
| | 1505 | 'terms' => array( $t ), |
| | 1506 | 'field' => 'term_id', |
| | 1507 | ), |
| | 1508 | ), |
| | 1509 | ) ); |
| | 1510 | |
| | 1511 | $this->assertEquals( $t, $q->get( 'term_id' ) ); |
| | 1512 | |
| | 1513 | _unregister_taxonomy( 'foo' ); |
| | 1514 | } |
| | 1515 | |
| | 1516 | /** |
| | 1517 | * @group taxonomy |
| | 1518 | * @ticket 29718 |
| | 1519 | */ |
| | 1520 | public function test_populate_cat_category_name_query_var_from_tax_query() { |
| | 1521 | register_taxonomy( 'foo', 'post' ); |
| | 1522 | $t = $this->factory->term->create( array( |
| | 1523 | 'taxonomy' => 'foo', |
| | 1524 | ) ); |
| | 1525 | $c = $this->factory->term->create( array( |
| | 1526 | 'taxonomy' => 'foo', |
| | 1527 | 'slug' => 'bar', |
| | 1528 | ) ); |
| | 1529 | |
| | 1530 | $q = new WP_Query( array( |
| | 1531 | 'tax_query' => array( |
| | 1532 | // Non-category should be skipped |
| | 1533 | array( |
| | 1534 | 'taxonomy' => 'foo', |
| | 1535 | 'terms' => array( $t ), |
| | 1536 | ), |
| | 1537 | |
| | 1538 | // Empty terms mean that this one should be skipped |
| | 1539 | array( |
| | 1540 | 'taxonomy' => 'category', |
| | 1541 | 'terms' => array(), |
| | 1542 | ), |
| | 1543 | |
| | 1544 | // Category and post tags should be skipped |
| | 1545 | array( |
| | 1546 | 'taxonomy' => 'category', |
| | 1547 | 'terms' => array( $c ), |
| | 1548 | ), |
| | 1549 | ), |
| | 1550 | ) ); |
| | 1551 | |
| | 1552 | $this->assertEquals( $c, $q->get( 'cat' ) ); |
| | 1553 | $this->assertEquals( 'bar', $q->get( 'category_name' ) ); |
| | 1554 | |
| | 1555 | _unregister_taxonomy( 'foo' ); |
| | 1556 | } |
| | 1557 | |
| | 1558 | /** |
| | 1559 | * @group taxonomy |
| | 1560 | * @ticket 29718 |
| | 1561 | */ |
| | 1562 | public function test_populate_tag_id_query_var_from_tax_query() { |
| | 1563 | register_taxonomy( 'foo', 'post' ); |
| | 1564 | $t = $this->factory->term->create( array( |
| | 1565 | 'taxonomy' => 'foo', |
| | 1566 | ) ); |
| | 1567 | $tag = $this->factory->term->create( array( |
| | 1568 | 'taxonomy' => 'post_tag', |
| | 1569 | 'slug' => 'bar', |
| | 1570 | ) ); |
| | 1571 | |
| | 1572 | $q = new WP_Query( array( |
| | 1573 | 'tax_query' => array( |
| | 1574 | // Non-tag should be skipped |
| | 1575 | array( |
| | 1576 | 'taxonomy' => 'foo', |
| | 1577 | 'terms' => array( $t ), |
| | 1578 | ), |
| | 1579 | |
| | 1580 | // Empty terms mean that this one should be skipped |
| | 1581 | array( |
| | 1582 | 'taxonomy' => 'post_tag', |
| | 1583 | 'terms' => array(), |
| | 1584 | ), |
| | 1585 | |
| | 1586 | // Category and post tags should be skipped |
| | 1587 | array( |
| | 1588 | 'taxonomy' => 'post_tag', |
| | 1589 | 'terms' => array( $tag ), |
| | 1590 | ), |
| | 1591 | ), |
| | 1592 | ) ); |
| | 1593 | |
| | 1594 | $this->assertEquals( $tag, $q->get( 'tag_id' ) ); |
| | 1595 | |
| | 1596 | _unregister_taxonomy( 'foo' ); |
| | 1597 | } |
| | 1598 | |
| | 1599 | /** |