Changeset 31286
- Timestamp:
- 01/27/2015 08:03:50 PM (10 years ago)
- Location:
- trunk/tests/phpunit/tests
- Files:
-
- 1 added
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/post/query.php
r30846 r31286 4 4 function setUp() { 5 5 parent::setUp(); 6 }7 8 /**9 * @group meta10 */11 public function test_meta_query_no_key() {12 $p1 = $this->factory->post->create();13 $p2 = $this->factory->post->create();14 $p3 = $this->factory->post->create();15 16 add_post_meta( $p1, 'foo', 'bar' );17 add_post_meta( $p2, 'oof', 'bar' );18 add_post_meta( $p3, 'oof', 'baz' );19 20 $query = new WP_Query( array(21 'update_post_meta_cache' => false,22 'update_post_term_cache' => false,23 'fields' => 'ids',24 'meta_query' => array(25 array(26 'value' => 'bar',27 ),28 ),29 ) );30 31 $expected = array( $p1, $p2 );32 $this->assertEqualSets( $expected, $query->posts );33 }34 35 /**36 * @group meta37 */38 public function test_meta_query_no_value() {39 $p1 = $this->factory->post->create();40 $p2 = $this->factory->post->create();41 $p3 = $this->factory->post->create();42 43 add_post_meta( $p1, 'foo', 'bar' );44 add_post_meta( $p2, 'oof', 'bar' );45 add_post_meta( $p3, 'oof', 'baz' );46 47 $query = new WP_Query( array(48 'update_post_meta_cache' => false,49 'update_post_term_cache' => false,50 'fields' => 'ids',51 'meta_query' => array(52 array(53 'key' => 'oof',54 ),55 ),56 ) );57 58 $expected = array( $p2, $p3 );59 $this->assertEqualSets( $expected, $query->posts );60 }61 62 /**63 * @group meta64 */65 public function test_meta_query_single_query_compare_default() {66 $p1 = $this->factory->post->create();67 $p2 = $this->factory->post->create();68 69 add_post_meta( $p1, 'foo', 'bar' );70 71 $query = new WP_Query( array(72 'update_post_meta_cache' => false,73 'update_post_term_cache' => false,74 'fields' => 'ids',75 'meta_query' => array(76 array(77 'key' => 'foo',78 'value' => 'bar',79 ),80 ),81 ) );82 83 $expected = array( $p1 );84 $this->assertEqualSets( $expected, $query->posts );85 }86 87 /**88 * @group meta89 */90 public function test_meta_query_single_query_compare_equals() {91 $p1 = $this->factory->post->create();92 $p2 = $this->factory->post->create();93 94 add_post_meta( $p1, 'foo', 'bar' );95 96 $query = new WP_Query( array(97 'update_post_meta_cache' => false,98 'update_post_term_cache' => false,99 'fields' => 'ids',100 'meta_query' => array(101 array(102 'key' => 'foo',103 'value' => 'bar',104 'compare' => '=',105 ),106 ),107 ) );108 109 $expected = array( $p1 );110 $this->assertEqualSets( $expected, $query->posts );111 }112 113 /**114 * @group meta115 */116 public function test_meta_query_single_query_compare_not_equals() {117 $p1 = $this->factory->post->create();118 $p2 = $this->factory->post->create();119 $p3 = $this->factory->post->create();120 121 add_post_meta( $p1, 'foo', 'bar' );122 add_post_meta( $p2, 'foo', 'baz' );123 124 $query = new WP_Query( array(125 'update_post_meta_cache' => false,126 'update_post_term_cache' => false,127 'fields' => 'ids',128 'meta_query' => array(129 array(130 'key' => 'foo',131 'value' => 'bar',132 'compare' => '!=',133 ),134 ),135 ) );136 137 $expected = array( $p2 );138 $this->assertEqualSets( $expected, $query->posts );139 }140 141 /**142 * @group meta143 */144 public function test_meta_query_single_query_compare_arithmetic_comparisons() {145 $p1 = $this->factory->post->create();146 $p2 = $this->factory->post->create();147 $p3 = $this->factory->post->create();148 149 add_post_meta( $p1, 'foo', '1' );150 add_post_meta( $p2, 'foo', '2' );151 add_post_meta( $p3, 'foo', '3' );152 153 // <154 $query = new WP_Query( array(155 'update_post_meta_cache' => false,156 'update_post_term_cache' => false,157 'fields' => 'ids',158 'meta_query' => array(159 array(160 'key' => 'foo',161 'value' => 2,162 'compare' => '<',163 ),164 ),165 ) );166 167 $expected = array( $p1 );168 $this->assertEqualSets( $expected, $query->posts );169 170 // <=171 $query = new WP_Query( array(172 'update_post_meta_cache' => false,173 'update_post_term_cache' => false,174 'fields' => 'ids',175 'meta_query' => array(176 array(177 'key' => 'foo',178 'value' => 2,179 'compare' => '<=',180 ),181 ),182 ) );183 184 $expected = array( $p1, $p2 );185 $this->assertEqualSets( $expected, $query->posts );186 187 // >=188 $query = new WP_Query( array(189 'update_post_meta_cache' => false,190 'update_post_term_cache' => false,191 'fields' => 'ids',192 'meta_query' => array(193 array(194 'key' => 'foo',195 'value' => 2,196 'compare' => '>=',197 ),198 ),199 ) );200 201 $expected = array( $p2, $p3 );202 $this->assertEqualSets( $expected, $query->posts );203 204 // >205 $query = new WP_Query( array(206 'update_post_meta_cache' => false,207 'update_post_term_cache' => false,208 'fields' => 'ids',209 'meta_query' => array(210 array(211 'key' => 'foo',212 'value' => 2,213 'compare' => '>',214 ),215 ),216 ) );217 218 $expected = array( $p3 );219 $this->assertEqualSets( $expected, $query->posts );220 }221 222 /**223 * @group meta224 */225 public function test_meta_query_single_query_compare_like() {226 $p1 = $this->factory->post->create();227 $p2 = $this->factory->post->create();228 229 add_post_meta( $p1, 'foo', 'bar' );230 231 $query = new WP_Query( array(232 'update_post_meta_cache' => false,233 'update_post_term_cache' => false,234 'fields' => 'ids',235 'meta_query' => array(236 array(237 'key' => 'foo',238 'value' => 'ba',239 'compare' => 'LIKE',240 ),241 ),242 ) );243 244 $expected = array( $p1 );245 $this->assertEqualSets( $expected, $query->posts );246 }247 248 /**249 * @group meta250 */251 public function test_meta_query_single_query_compare_not_like() {252 $p1 = $this->factory->post->create();253 $p2 = $this->factory->post->create();254 $p3 = $this->factory->post->create();255 256 add_post_meta( $p1, 'foo', 'bar' );257 add_post_meta( $p2, 'foo', 'rab' );258 259 $query = new WP_Query( array(260 'update_post_meta_cache' => false,261 'update_post_term_cache' => false,262 'fields' => 'ids',263 'meta_query' => array(264 array(265 'key' => 'foo',266 'value' => 'ba',267 'compare' => 'NOT LIKE',268 ),269 ),270 ) );271 272 $expected = array( $p2 );273 $this->assertEqualSets( $expected, $query->posts );274 }275 276 /**277 * @group meta278 */279 public function test_meta_query_single_query_compare_between_not_between() {280 $p1 = $this->factory->post->create();281 $p2 = $this->factory->post->create();282 $p3 = $this->factory->post->create();283 284 add_post_meta( $p1, 'foo', '1' );285 add_post_meta( $p2, 'foo', '10' );286 add_post_meta( $p3, 'foo', '100' );287 288 $query = new WP_Query( array(289 'update_post_meta_cache' => false,290 'update_post_term_cache' => false,291 'fields' => 'ids',292 'meta_query' => array(293 array(294 'key' => 'foo',295 'value' => array( 9, 12 ),296 'compare' => 'BETWEEN',297 'type' => 'NUMERIC',298 ),299 ),300 ) );301 302 $expected = array( $p2 );303 $this->assertEqualSets( $expected, $query->posts );304 305 $query = new WP_Query( array(306 'update_post_meta_cache' => false,307 'update_post_term_cache' => false,308 'fields' => 'ids',309 'meta_query' => array(310 array(311 'key' => 'foo',312 'value' => array( 9, 12 ),313 'compare' => 'NOT BETWEEN',314 'type' => 'NUMERIC',315 ),316 ),317 ) );318 319 $expected = array( $p1, $p3 );320 $this->assertEqualSets( $expected, $query->posts );321 }322 323 /**324 * @group meta325 */326 public function test_meta_query_single_query_compare_regexp_rlike() {327 $p1 = $this->factory->post->create();328 $p2 = $this->factory->post->create();329 330 add_post_meta( $p1, 'foo', 'bar' );331 add_post_meta( $p2, 'foo', 'baz' );332 333 $query = new WP_Query( array(334 'update_post_meta_cache' => false,335 'update_post_term_cache' => false,336 'fields' => 'ids',337 'meta_query' => array(338 array(339 'key' => 'foo',340 'value' => 'z$',341 'compare' => 'REGEXP',342 ),343 ),344 ) );345 346 $expected = array( $p2 );347 $this->assertEqualSets( $expected, $query->posts );348 349 // RLIKE is a synonym for REGEXP.350 $query = new WP_Query( array(351 'update_post_meta_cache' => false,352 'update_post_term_cache' => false,353 'fields' => 'ids',354 'meta_query' => array(355 array(356 'key' => 'foo',357 'value' => 'z$',358 'compare' => 'RLIKE',359 ),360 ),361 ) );362 363 $expected = array( $p2 );364 $this->assertEqualSets( $expected, $query->posts );365 }366 367 /**368 * @group meta369 */370 public function test_meta_query_single_query_compare_not_regexp() {371 $p1 = $this->factory->post->create();372 $p2 = $this->factory->post->create();373 374 add_post_meta( $p1, 'foo', 'bar' );375 add_post_meta( $p2, 'foo', 'baz' );376 377 $query = new WP_Query( array(378 'update_post_meta_cache' => false,379 'update_post_term_cache' => false,380 'fields' => 'ids',381 'meta_query' => array(382 array(383 'key' => 'foo',384 'value' => 'z$',385 'compare' => 'NOT REGEXP',386 ),387 ),388 ) );389 390 $expected = array( $p1 );391 $this->assertEqualSets( $expected, $query->posts );392 }393 394 /**395 * @group meta396 */397 public function test_meta_query_relation_default() {398 $p1 = $this->factory->post->create();399 $p2 = $this->factory->post->create();400 $p3 = $this->factory->post->create();401 402 add_post_meta( $p1, 'foo', 'foo value 1' );403 add_post_meta( $p1, 'bar', 'bar value 1' );404 add_post_meta( $p2, 'foo', 'foo value 1' );405 add_post_meta( $p2, 'bar', 'bar value 2' );406 407 $query = new WP_Query( array(408 'update_post_meta_cache' => false,409 'update_post_term_cache' => false,410 'fields' => 'ids',411 'meta_query' => array(412 array(413 'key' => 'foo',414 'value' => 'foo value 1',415 ),416 array(417 'key' => 'bar',418 'value' => 'bar value 1',419 ),420 ),421 ) );422 423 $expected = array( $p1 );424 $this->assertEquals( $expected, $query->posts );425 }426 427 /**428 * @group meta429 */430 public function test_meta_query_relation_or() {431 $post_id = $this->factory->post->create();432 add_post_meta( $post_id, 'foo', rand_str() );433 add_post_meta( $post_id, 'foo', rand_str() );434 $post_id2 = $this->factory->post->create();435 add_post_meta( $post_id2, 'bar', 'val2' );436 $post_id3 = $this->factory->post->create();437 add_post_meta( $post_id3, 'baz', rand_str() );438 $post_id4 = $this->factory->post->create();439 add_post_meta( $post_id4, 'froo', rand_str() );440 $post_id5 = $this->factory->post->create();441 add_post_meta( $post_id5, 'tango', 'val2' );442 $post_id6 = $this->factory->post->create();443 add_post_meta( $post_id6, 'bar', 'val1' );444 445 $query = new WP_Query( array(446 'update_post_meta_cache' => false,447 'update_post_term_cache' => false,448 'fields' => 'ids',449 'meta_query' => array(450 array(451 'key' => 'foo'452 ),453 array(454 'key' => 'bar',455 'value' => 'val2'456 ),457 array(458 'key' => 'baz'459 ),460 array(461 'key' => 'froo'462 ),463 'relation' => 'OR',464 ),465 ) );466 467 $expected = array( $post_id, $post_id2, $post_id3, $post_id4 );468 $this->assertEqualSets( $expected, $query->posts );469 }470 471 /**472 * @group meta473 */474 public function test_meta_query_relation_and() {475 $post_id = $this->factory->post->create();476 add_post_meta( $post_id, 'foo', rand_str() );477 add_post_meta( $post_id, 'foo', rand_str() );478 $post_id2 = $this->factory->post->create();479 add_post_meta( $post_id2, 'bar', 'val2' );480 add_post_meta( $post_id2, 'foo', rand_str() );481 $post_id3 = $this->factory->post->create();482 add_post_meta( $post_id3, 'baz', rand_str() );483 $post_id4 = $this->factory->post->create();484 add_post_meta( $post_id4, 'froo', rand_str() );485 $post_id5 = $this->factory->post->create();486 add_post_meta( $post_id5, 'tango', 'val2' );487 $post_id6 = $this->factory->post->create();488 add_post_meta( $post_id6, 'bar', 'val1' );489 add_post_meta( $post_id6, 'foo', rand_str() );490 $post_id7 = $this->factory->post->create();491 add_post_meta( $post_id7, 'foo', rand_str() );492 add_post_meta( $post_id7, 'froo', rand_str() );493 add_post_meta( $post_id7, 'baz', rand_str() );494 add_post_meta( $post_id7, 'bar', 'val2' );495 496 $query = new WP_Query( array(497 'meta_query' => array(498 array(499 'key' => 'foo'500 ),501 array(502 'key' => 'bar',503 'value' => 'val2'504 ),505 array(506 'key' => 'baz'507 ),508 array(509 'key' => 'froo'510 ),511 'relation' => 'AND',512 ),513 'update_post_meta_cache' => false,514 'update_post_term_cache' => false,515 'fields' => 'ids',516 ) );517 518 $expected = array( $post_id7 );519 $this->assertEqualSets( $expected, $query->posts );520 521 $query = new WP_Query( array(522 'meta_query' => array(523 array(524 'key' => 'foo'525 ),526 array(527 'key' => 'bar',528 ),529 'relation' => 'AND',530 ),531 'update_post_meta_cache' => false,532 'update_post_term_cache' => false,533 'fields' => 'ids',534 ) );535 536 $expected = array( $post_id2, $post_id6, $post_id7 );537 $this->assertEqualSets( $expected, $query->posts );538 }539 540 /**541 * @ticket 30681542 */543 public function test_meta_query_compare_exists() {544 $posts = $this->factory->post->create_many( 3 );545 add_post_meta( $posts[0], 'foo', 'bar' );546 add_post_meta( $posts[2], 'foo', 'baz' );547 548 $query = new WP_Query( array(549 'fields' => 'ids',550 'meta_query' => array(551 array(552 'compare' => 'EXISTS',553 'key' => 'foo',554 ),555 ),556 ) );557 558 $this->assertEqualSets( array( $posts[0], $posts[2] ), $query->posts );559 }560 561 /**562 * @ticket 30681563 */564 public function test_meta_query_compare_exists_with_value_should_convert_to_equals() {565 $posts = $this->factory->post->create_many( 3 );566 add_post_meta( $posts[0], 'foo', 'bar' );567 add_post_meta( $posts[2], 'foo', 'baz' );568 569 $query = new WP_Query( array(570 'fields' => 'ids',571 'meta_query' => array(572 array(573 'compare' => 'EXISTS',574 'value' => 'baz',575 'key' => 'foo',576 ),577 ),578 ) );579 580 $this->assertEqualSets( array( $posts[2] ), $query->posts );581 }582 583 /**584 * @ticket 30681585 */586 public function test_meta_query_compare_not_exists_should_ignore_value() {587 $posts = $this->factory->post->create_many( 3 );588 add_post_meta( $posts[0], 'foo', 'bar' );589 add_post_meta( $posts[2], 'foo', 'baz' );590 591 $query = new WP_Query( array(592 'fields' => 'ids',593 'meta_query' => array(594 array(595 'compare' => 'NOT EXISTS',596 'value' => 'bar',597 'key' => 'foo',598 ),599 ),600 ) );601 602 $this->assertEqualSets( array( $posts[1] ), $query->posts );603 }604 605 /**606 * @ticket 18158607 * @group meta608 */609 public function test_meta_query_compare_not_exists() {610 $post_id = $this->factory->post->create();611 add_post_meta( $post_id, 'foo', rand_str() );612 $post_id2 = $this->factory->post->create();613 add_post_meta( $post_id2, 'bar', rand_str() );614 $post_id3 = $this->factory->post->create();615 add_post_meta( $post_id3, 'bar', rand_str() );616 $post_id4 = $this->factory->post->create();617 add_post_meta( $post_id4, 'baz', rand_str() );618 $post_id5 = $this->factory->post->create();619 add_post_meta( $post_id5, 'foo', rand_str() );620 621 $query = new WP_Query( array(622 'meta_query' => array(623 array(624 'key' => 'foo',625 'compare' => 'NOT EXISTS',626 ),627 ),628 'update_post_meta_cache' => false,629 'update_post_term_cache' => false,630 'fields' => 'ids',631 ) );632 633 $expected = array( $post_id2, $post_id3, $post_id4 );634 $this->assertEqualSets( $expected, $query->posts );635 636 $query = new WP_Query( array(637 'meta_query' => array(638 array(639 'key' => 'foo',640 'compare' => 'NOT EXISTS',641 ),642 array(643 'key' => 'bar',644 'compare' => 'NOT EXISTS',645 ),646 ),647 'update_post_meta_cache' => false,648 'update_post_term_cache' => false,649 'fields' => 'ids',650 ) );651 652 $expected = array( $post_id4 );653 $this->assertEquals( $expected, $query->posts );654 655 $query = new WP_Query( array(656 'meta_query' => array(657 array(658 'key' => 'foo',659 'compare' => 'NOT EXISTS',660 ),661 array(662 'key' => 'bar',663 'compare' => 'NOT EXISTS',664 ),665 array(666 'key' => 'baz',667 'compare' => 'NOT EXISTS',668 ),669 ),670 'update_post_meta_cache' => false,671 'update_post_term_cache' => false,672 'fields' => 'ids',673 ) );674 675 $this->assertEquals( 0, count( $query->posts ) );676 }677 678 /**679 * @ticket 29062680 */681 public function test_meta_query_compare_not_exists_with_another_condition_relation_or() {682 $posts = $this->factory->post->create_many( 4 );683 update_post_meta( $posts[0], 'color', 'orange' );684 update_post_meta( $posts[1], 'color', 'blue' );685 update_post_meta( $posts[1], 'vegetable', 'onion' );686 update_post_meta( $posts[2], 'vegetable', 'shallot' );687 688 $post_3_meta = get_post_meta( $posts[3] );689 foreach ( $post_3_meta as $meta_key => $meta_value ) {690 delete_post_meta( $posts[3], $meta_key );691 }692 693 $query = new WP_Query( array(694 'meta_query' => array(695 'relation' => 'OR',696 array(697 'key' => 'vegetable',698 'value' => 'onion',699 ),700 array(701 'key' => 'color',702 'compare' => 'NOT EXISTS',703 ),704 ),705 'update_post_meta_cache' => false,706 'update_post_term_cache' => false,707 'fields' => 'ids',708 ) );709 710 $expected = array( $posts[1], $posts[2], $posts[3] );711 $this->assertEqualSets( $expected, $query->posts );712 }713 714 /**715 * @ticket 24093716 */717 public function test_meta_query_relation_or_compare_equals() {718 $posts = $this->factory->post->create_many( 4 );719 add_post_meta( $posts[0], 'color', 'orange' );720 add_post_meta( $posts[1], 'color', 'blue' );721 add_post_meta( $posts[1], 'vegetable', 'onion' );722 add_post_meta( $posts[2], 'vegetable', 'shallot' );723 724 $query = new WP_Query( array(725 'meta_query' => array(726 'relation' => 'OR',727 array(728 'key' => 'vegetable',729 'value' => 'onion',730 'compare' => '=',731 ),732 array(733 'key' => 'vegetable',734 'value' => 'shallot',735 'compare' => '=',736 ),737 ),738 'update_post_meta_cache' => false,739 'update_post_term_cache' => false,740 'fields' => 'ids',741 ) );742 743 $expected = array( $posts[1], $posts[2] );744 $this->assertEqualSets( $expected, $query->posts );745 }746 747 /**748 * @ticket 24093749 */750 public function test_meta_query_relation_or_compare_equals_different_keys() {751 $posts = $this->factory->post->create_many( 4 );752 add_post_meta( $posts[0], 'color', 'orange' );753 add_post_meta( $posts[1], 'color', 'blue' );754 add_post_meta( $posts[1], 'vegetable', 'onion' );755 add_post_meta( $posts[2], 'vegetable', 'shallot' );756 757 $query = new WP_Query( array(758 'meta_query' => array(759 'relation' => 'OR',760 array(761 'key' => 'vegetable',762 'value' => 'onion',763 'compare' => '=',764 ),765 array(766 'key' => 'color',767 'value' => 'orange',768 'compare' => '=',769 ),770 ),771 'update_post_meta_cache' => false,772 'update_post_term_cache' => false,773 'fields' => 'ids',774 ) );775 776 $expected = array( $posts[0], $posts[1] );777 $this->assertEqualSets( $expected, $query->posts );778 }779 780 /**781 * @ticket 24093782 */783 public function test_meta_query_relation_or_compare_equals_and_in() {784 $posts = $this->factory->post->create_many( 4 );785 add_post_meta( $posts[0], 'color', 'orange' );786 add_post_meta( $posts[1], 'color', 'blue' );787 add_post_meta( $posts[1], 'vegetable', 'onion' );788 add_post_meta( $posts[2], 'vegetable', 'shallot' );789 790 $query = new WP_Query( array(791 'meta_query' => array(792 'relation' => 'OR',793 array(794 'key' => 'vegetable',795 'value' => 'onion',796 'compare' => '=',797 ),798 array(799 'key' => 'color',800 'value' => array( 'orange', 'green' ),801 'compare' => 'IN',802 ),803 ),804 'update_post_meta_cache' => false,805 'update_post_term_cache' => false,806 'fields' => 'ids',807 ) );808 809 $expected = array( $posts[0], $posts[1] );810 $this->assertEqualSets( $expected, $query->posts );811 }812 813 /**814 * @ticket 24093815 */816 public function test_meta_query_relation_or_compare_equals_and_like() {817 $posts = $this->factory->post->create_many( 4 );818 add_post_meta( $posts[0], 'color', 'orange' );819 add_post_meta( $posts[1], 'color', 'blue' );820 add_post_meta( $posts[1], 'vegetable', 'onion' );821 add_post_meta( $posts[2], 'vegetable', 'shallot' );822 823 $query = new WP_Query( array(824 'meta_query' => array(825 'relation' => 'OR',826 array(827 'key' => 'vegetable',828 'value' => 'onion',829 'compare' => '=',830 ),831 array(832 'key' => 'vegetable',833 'value' => 'hall',834 'compare' => 'LIKE',835 ),836 ),837 'update_post_meta_cache' => false,838 'update_post_term_cache' => false,839 'fields' => 'ids',840 ) );841 842 $expected = array( $posts[1], $posts[2] );843 $this->assertEqualSets( $expected, $query->posts );844 }845 846 /**847 * @ticket 24093848 */849 public function test_meta_query_relation_or_compare_equals_and_between() {850 $posts = $this->factory->post->create_many( 4 );851 add_post_meta( $posts[0], 'number_of_colors', '2' );852 add_post_meta( $posts[1], 'number_of_colors', '5' );853 add_post_meta( $posts[1], 'vegetable', 'onion' );854 add_post_meta( $posts[2], 'vegetable', 'shallot' );855 856 $query = new WP_Query( array(857 'meta_query' => array(858 'relation' => 'OR',859 array(860 'key' => 'vegetable',861 'value' => 'shallot',862 'compare' => '=',863 ),864 array(865 'key' => 'number_of_colors',866 'value' => array( 1, 3 ),867 'compare' => 'BETWEEN',868 'type' => 'SIGNED',869 ),870 ),871 'update_post_meta_cache' => false,872 'update_post_term_cache' => false,873 'fields' => 'ids',874 ) );875 876 $expected = array( $posts[0], $posts[2] );877 $this->assertEqualSets( $expected, $query->posts );878 }879 880 /**881 * @ticket 24093882 */883 public function test_meta_query_relation_and_compare_in_same_keys() {884 $posts = $this->factory->post->create_many( 4 );885 add_post_meta( $posts[0], 'color', 'orange' );886 add_post_meta( $posts[1], 'color', 'blue' );887 add_post_meta( $posts[1], 'vegetable', 'onion' );888 add_post_meta( $posts[2], 'vegetable', 'shallot' );889 add_post_meta( $posts[3], 'vegetable', 'banana' );890 add_post_meta( $posts[3], 'vegetable', 'onion' );891 892 $query = new WP_Query( array(893 'meta_query' => array(894 'relation' => 'AND',895 array(896 'key' => 'vegetable',897 'value' => array( 'onion', 'shallot' ),898 'compare' => 'IN',899 ),900 array(901 'key' => 'vegetable',902 'value' => array( 'banana' ),903 'compare' => 'IN',904 ),905 ),906 'update_post_meta_cache' => false,907 'update_post_term_cache' => false,908 'fields' => 'ids',909 ) );910 911 $expected = array( $posts[3] );912 $this->assertEqualSets( $expected, $query->posts );913 }914 915 /**916 * @ticket 24093917 */918 public function test_meta_query_relation_and_compare_in_different_keys() {919 $posts = $this->factory->post->create_many( 4 );920 add_post_meta( $posts[0], 'color', 'orange' );921 add_post_meta( $posts[1], 'color', 'blue' );922 add_post_meta( $posts[1], 'vegetable', 'onion' );923 add_post_meta( $posts[1], 'vegetable', 'shallot' );924 add_post_meta( $posts[2], 'vegetable', 'shallot' );925 add_post_meta( $posts[3], 'vegetable', 'banana' );926 927 $query = new WP_Query( array(928 'meta_query' => array(929 'relation' => 'AND',930 array(931 'key' => 'vegetable',932 'value' => array( 'onion', 'shallot' ),933 'compare' => 'IN',934 ),935 array(936 'key' => 'color',937 'value' => array( 'blue' ),938 'compare' => 'IN',939 ),940 ),941 'update_post_meta_cache' => false,942 'update_post_term_cache' => false,943 'fields' => 'ids',944 ) );945 946 $expected = array( $posts[1] );947 $this->assertEqualSets( $expected, $query->posts );948 }949 950 /**951 * @ticket 24093952 */953 public function test_meta_query_relation_and_compare_not_equals() {954 $posts = $this->factory->post->create_many( 4 );955 add_post_meta( $posts[0], 'color', 'orange' );956 add_post_meta( $posts[1], 'color', 'blue' );957 add_post_meta( $posts[1], 'vegetable', 'onion' );958 add_post_meta( $posts[2], 'vegetable', 'shallot' );959 add_post_meta( $posts[3], 'vegetable', 'banana' );960 961 $query = new WP_Query( array(962 'meta_query' => array(963 'relation' => 'AND',964 array(965 'key' => 'vegetable',966 'value' => 'onion',967 'compare' => '!=',968 ),969 array(970 'key' => 'vegetable',971 'value' => 'shallot',972 'compare' => '!=',973 ),974 ),975 'update_post_meta_cache' => false,976 'update_post_term_cache' => false,977 'fields' => 'ids',978 ) );979 980 $expected = array( $posts[3] );981 $this->assertEqualSets( $expected, $query->posts );982 }983 984 /**985 * @ticket 24093986 */987 public function test_meta_query_relation_and_compare_not_equals_different_keys() {988 $posts = $this->factory->post->create_many( 4 );989 990 // !shallot, but orange.991 add_post_meta( $posts[0], 'color', 'orange' );992 add_post_meta( $posts[0], 'vegetable', 'onion' );993 994 // !orange, but shallot.995 add_post_meta( $posts[1], 'color', 'blue' );996 add_post_meta( $posts[1], 'vegetable', 'shallot' );997 998 // Neither.999 add_post_meta( $posts[2], 'color', 'blue' );1000 add_post_meta( $posts[2], 'vegetable', 'onion' );1001 1002 $query = new WP_Query( array(1003 'meta_query' => array(1004 'relation' => 'AND',1005 array(1006 'key' => 'vegetable',1007 'value' => 'shallot',1008 'compare' => '!=',1009 ),1010 array(1011 'key' => 'color',1012 'value' => 'orange',1013 'compare' => '!=',1014 ),1015 ),1016 'update_post_meta_cache' => false,1017 'update_post_term_cache' => false,1018 'fields' => 'ids',1019 ) );1020 1021 $expected = array( $posts[2] );1022 $this->assertEqualSets( $expected, $query->posts );1023 }1024 1025 /**1026 * @ticket 240931027 */1028 public function test_meta_query_relation_and_compare_not_equals_not_in() {1029 $posts = $this->factory->post->create_many( 4 );1030 add_post_meta( $posts[0], 'color', 'orange' );1031 add_post_meta( $posts[1], 'color', 'blue' );1032 add_post_meta( $posts[1], 'vegetable', 'onion' );1033 add_post_meta( $posts[2], 'vegetable', 'shallot' );1034 add_post_meta( $posts[3], 'vegetable', 'banana' );1035 1036 $query = new WP_Query( array(1037 'meta_query' => array(1038 'relation' => 'AND',1039 array(1040 'key' => 'vegetable',1041 'value' => 'onion',1042 'compare' => '!=',1043 ),1044 array(1045 'key' => 'vegetable',1046 'value' => array( 'shallot' ),1047 'compare' => 'NOT IN',1048 ),1049 ),1050 'update_post_meta_cache' => false,1051 'update_post_term_cache' => false,1052 'fields' => 'ids',1053 ) );1054 1055 $expected = array( $posts[3] );1056 $this->assertEqualSets( $expected, $query->posts );1057 }1058 1059 /**1060 * @ticket 240931061 */1062 public function test_meta_query_relation_and_compare_not_equals_and_not_like() {1063 $posts = $this->factory->post->create_many( 4 );1064 add_post_meta( $posts[0], 'color', 'orange' );1065 add_post_meta( $posts[1], 'color', 'blue' );1066 add_post_meta( $posts[1], 'vegetable', 'onion' );1067 add_post_meta( $posts[2], 'vegetable', 'shallot' );1068 add_post_meta( $posts[3], 'vegetable', 'banana' );1069 1070 $query = new WP_Query( array(1071 'meta_query' => array(1072 'relation' => 'AND',1073 array(1074 'key' => 'vegetable',1075 'value' => 'onion',1076 'compare' => '!=',1077 ),1078 array(1079 'key' => 'vegetable',1080 'value' => 'hall',1081 'compare' => 'NOT LIKE',1082 ),1083 ),1084 'update_post_meta_cache' => false,1085 'update_post_term_cache' => false,1086 'fields' => 'ids',1087 ) );1088 1089 $expected = array( $posts[3] );1090 $this->assertEqualSets( $expected, $query->posts );1091 }1092 1093 /**1094 * @ticket 230331095 * @group meta1096 */1097 public function test_meta_query_decimal_results() {1098 $post_1 = $this->factory->post->create();1099 $post_2 = $this->factory->post->create();1100 $post_3 = $this->factory->post->create();1101 $post_4 = $this->factory->post->create();1102 1103 update_post_meta( $post_1, 'decimal_value', '-0.3' );1104 update_post_meta( $post_2, 'decimal_value', '0.23409844' );1105 update_post_meta( $post_3, 'decimal_value', '0.3' );1106 update_post_meta( $post_4, 'decimal_value', '0.4' );1107 1108 $query = new WP_Query( array(1109 'meta_query' => array(1110 array(1111 'key' => 'decimal_value',1112 'value' => '.300',1113 'compare' => '=',1114 'type' => 'DECIMAL(10,2)'1115 )1116 ),1117 'update_post_meta_cache' => false,1118 'update_post_term_cache' => false,1119 'fields' => 'ids',1120 ) );1121 $this->assertEqualSets( array( $post_3 ), $query->posts );1122 1123 $query = new WP_Query( array(1124 'meta_query' => array(1125 array(1126 'key' => 'decimal_value',1127 'value' => '0.35',1128 'compare' => '>',1129 'type' => 'DECIMAL(10,2)'1130 )1131 ),1132 'update_post_meta_cache' => false,1133 'update_post_term_cache' => false,1134 'fields' => 'ids',1135 ) );1136 $this->assertEqualSets( array( $post_4 ), $query->posts );1137 1138 $query = new WP_Query( array(1139 'meta_query' => array(1140 array(1141 'key' => 'decimal_value',1142 'value' => '0.3',1143 'compare' => '>=',1144 'type' => 'DECIMAL(10,2)'1145 )1146 ),1147 'update_post_meta_cache' => false,1148 'update_post_term_cache' => false,1149 'fields' => 'ids',1150 ) );1151 $this->assertEqualSets( array( $post_3, $post_4 ), $query->posts );1152 1153 $query = new WP_Query( array(1154 'meta_query' => array(1155 array(1156 'key' => 'decimal_value',1157 'value' => '0',1158 'compare' => '<',1159 'type' => 'DECIMAL(10,2)'1160 )1161 ),1162 'update_post_meta_cache' => false,1163 'update_post_term_cache' => false,1164 'fields' => 'ids',1165 ) );1166 $this->assertEqualSets( array( $post_1 ), $query->posts, 'ID' );1167 1168 $query = new WP_Query( array(1169 'meta_query' => array(1170 array(1171 'key' => 'decimal_value',1172 'value' => '0.3',1173 'compare' => '<=',1174 'type' => 'DECIMAL(10,2)'1175 )1176 ),1177 'update_post_meta_cache' => false,1178 'update_post_term_cache' => false,1179 'fields' => 'ids',1180 ) );1181 $this->assertEqualSets( array( $post_1, $post_2, $post_3 ), $query->posts );1182 1183 $query = new WP_Query( array(1184 'meta_query' => array(1185 array(1186 'key' => 'decimal_value',1187 'value' => array( 0.23409845, .31 ),1188 'compare' => 'BETWEEN',1189 'type' => 'DECIMAL(10, 10)'1190 )1191 ),1192 'update_post_meta_cache' => false,1193 'update_post_term_cache' => false,1194 'fields' => 'ids',1195 ) );1196 $this->assertEqualSets( array( $post_3 ), $query->posts );1197 1198 $query = new WP_Query( array(1199 'meta_query' => array(1200 array(1201 'key' => 'decimal_value',1202 'value' => array( 0.23409845, .31 ),1203 'compare' => 'NOT BETWEEN',1204 'type' => 'DECIMAL(10,10)'1205 )1206 ),1207 'update_post_meta_cache' => false,1208 'update_post_term_cache' => false,1209 'fields' => 'ids',1210 ) );1211 $this->assertEqualSets( array( $post_1, $post_2, $post_4 ), $query->posts );1212 1213 $query = new WP_Query( array(1214 'meta_query' => array(1215 array(1216 'key' => 'decimal_value',1217 'value' => '.3',1218 'compare' => 'LIKE',1219 'type' => 'DECIMAL(10,2)'1220 )1221 ),1222 'update_post_meta_cache' => false,1223 'update_post_term_cache' => false,1224 'fields' => 'ids',1225 ) );1226 $this->assertEqualSets( array( $post_1, $post_3 ), $query->posts );1227 1228 $query = new WP_Query( array(1229 'meta_query' => array(1230 array(1231 'key' => 'decimal_value',1232 'value' => '.3',1233 'compare' => 'NOT LIKE',1234 'type' => 'DECIMAL(10,2)'1235 )1236 ),1237 'update_post_meta_cache' => false,1238 'update_post_term_cache' => false,1239 'fields' => 'ids',1240 ) );1241 $this->assertEqualSets( array( $post_2, $post_4 ), $query->posts );1242 1243 $query = new WP_Query( array(1244 'orderby' => 'meta_value',1245 'order' => 'DESC',1246 'meta_key' => 'decimal_value',1247 'meta_type' => 'DECIMAL(10, 2)',1248 'update_post_meta_cache' => false,1249 'update_post_term_cache' => false,1250 'fields' => 'ids',1251 ) );1252 $this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), $query->posts );1253 }1254 1255 /**1256 * @group meta1257 * @ticket 296041258 */1259 public function test_meta_query_with_orderby_meta_value_relation_or() {1260 $posts = $this->factory->post->create_many( 4 );1261 update_post_meta( $posts[0], 'foo', 5 );1262 update_post_meta( $posts[1], 'foo', 6 );1263 update_post_meta( $posts[2], 'foo', 4 );1264 update_post_meta( $posts[3], 'foo', 7 );1265 1266 update_post_meta( $posts[0], 'bar1', 'baz' );1267 update_post_meta( $posts[1], 'bar1', 'baz' );1268 update_post_meta( $posts[2], 'bar2', 'baz' );1269 1270 $query = new WP_Query( array(1271 'orderby' => 'meta_value',1272 'order' => 'ASC',1273 'meta_key' => 'foo',1274 'meta_query' => array(1275 'relation' => 'OR',1276 array(1277 'key' => 'bar1',1278 'value' => 'baz',1279 'compare' => '=',1280 ),1281 array(1282 'key' => 'bar2',1283 'value' => 'baz',1284 'compare' => '=',1285 ),1286 ),1287 'update_post_meta_cache' => false,1288 'update_post_term_cache' => false,1289 'fields' => 'ids',1290 ) );1291 1292 $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $query->posts );1293 }1294 1295 /**1296 * @group meta1297 * @ticket 296041298 */1299 public function test_meta_query_with_orderby_meta_value_relation_and() {1300 $posts = $this->factory->post->create_many( 4 );1301 update_post_meta( $posts[0], 'foo', 5 );1302 update_post_meta( $posts[1], 'foo', 6 );1303 update_post_meta( $posts[2], 'foo', 4 );1304 update_post_meta( $posts[3], 'foo', 7 );1305 1306 update_post_meta( $posts[0], 'bar1', 'baz' );1307 update_post_meta( $posts[1], 'bar1', 'baz' );1308 update_post_meta( $posts[2], 'bar1', 'baz' );1309 update_post_meta( $posts[3], 'bar1', 'baz' );1310 update_post_meta( $posts[0], 'bar2', 'baz' );1311 update_post_meta( $posts[1], 'bar2', 'baz' );1312 update_post_meta( $posts[2], 'bar2', 'baz' );1313 1314 $query = new WP_Query( array(1315 'orderby' => 'meta_value',1316 'order' => 'ASC',1317 'meta_key' => 'foo',1318 'meta_query' => array(1319 'relation' => 'AND',1320 array(1321 'key' => 'bar1',1322 'value' => 'baz',1323 'compare' => '=',1324 ),1325 array(1326 'key' => 'bar2',1327 'value' => 'baz',1328 'compare' => '=',1329 ),1330 ),1331 'update_post_meta_cache' => false,1332 'update_post_term_cache' => false,1333 'fields' => 'ids',1334 ) );1335 1336 $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $query->posts );1337 }1338 1339 /**1340 * @ticket 296421341 * @group meta1342 */1343 public function test_meta_query_nested() {1344 $p1 = $this->factory->post->create();1345 $p2 = $this->factory->post->create();1346 $p3 = $this->factory->post->create();1347 1348 add_post_meta( $p1, 'foo', 'bar' );1349 add_post_meta( $p2, 'foo2', 'bar' );1350 add_post_meta( $p3, 'foo2', 'bar' );1351 add_post_meta( $p3, 'foo3', 'bar' );1352 1353 $query = new WP_Query( array(1354 'update_post_meta_cache' => false,1355 'update_term_meta_cache' => false,1356 'fields' => 'ids',1357 'meta_query' => array(1358 'relation' => 'OR',1359 array(1360 'key' => 'foo',1361 'value' => 'bar',1362 ),1363 array(1364 'relation' => 'AND',1365 array(1366 'key' => 'foo2',1367 'value' => 'bar',1368 ),1369 array(1370 'key' => 'foo3',1371 'value' => 'bar',1372 ),1373 ),1374 ),1375 ) );1376 1377 $expected = array( $p1, $p3 );1378 $this->assertEqualSets( $expected, $query->posts );1379 }1380 1381 /**1382 * @ticket 296421383 * @group meta1384 */1385 public function test_meta_query_nested_two_levels_deep() {1386 $p1 = $this->factory->post->create();1387 $p2 = $this->factory->post->create();1388 $p3 = $this->factory->post->create();1389 1390 add_post_meta( $p1, 'foo', 'bar' );1391 add_post_meta( $p3, 'foo2', 'bar' );1392 add_post_meta( $p3, 'foo3', 'bar' );1393 add_post_meta( $p3, 'foo4', 'bar' );1394 1395 $query = new WP_Query( array(1396 'update_post_meta_cache' => false,1397 'update_term_meta_cache' => false,1398 'fields' => 'ids',1399 'meta_query' => array(1400 'relation' => 'OR',1401 array(1402 'key' => 'foo',1403 'value' => 'bar',1404 ),1405 array(1406 'relation' => 'OR',1407 array(1408 'key' => 'foo2',1409 'value' => 'bar',1410 ),1411 array(1412 'relation' => 'AND',1413 array(1414 'key' => 'foo3',1415 'value' => 'bar',1416 ),1417 array(1418 'key' => 'foo4',1419 'value' => 'bar',1420 ),1421 ),1422 ),1423 ),1424 ) );1425 1426 $expected = array( $p1, $p3 );1427 $this->assertEqualSets( $expected, $query->posts );1428 }1429 1430 /**1431 * @group meta1432 */1433 function test_meta_between_not_between() {1434 $post_id = $this->factory->post->create();1435 add_post_meta( $post_id, 'time', 500 );1436 $post_id2 = $this->factory->post->create();1437 add_post_meta( $post_id2, 'time', 1001 );1438 $post_id3 = $this->factory->post->create();1439 add_post_meta( $post_id3, 'time', 0 );1440 $post_id4 = $this->factory->post->create();1441 add_post_meta( $post_id4, 'time', 1 );1442 $post_id5 = $this->factory->post->create();1443 add_post_meta( $post_id5, 'time', 1000 );1444 1445 $args = array(1446 'meta_key' => 'time',1447 'meta_value' => array( 1, 1000 ),1448 'meta_type' => 'numeric',1449 'meta_compare' => 'NOT BETWEEN'1450 );1451 1452 $query = new WP_Query( $args );1453 $this->assertEquals( 2, count ( $query->posts ) );1454 foreach ( $query->posts as $post ) {1455 $this->assertInstanceOf( 'WP_Post', $post );1456 $this->assertEquals( 'raw', $post->filter );1457 }1458 $posts = wp_list_pluck( $query->posts, 'ID' );1459 $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts );1460 1461 $args = array(1462 'meta_key' => 'time',1463 'meta_value' => array( 1, 1000 ),1464 'meta_type' => 'numeric',1465 'meta_compare' => 'BETWEEN'1466 );1467 1468 $query = new WP_Query( $args );1469 $this->assertEquals( 3, count ( $query->posts ) );1470 foreach ( $query->posts as $post ) {1471 $this->assertInstanceOf( 'WP_Post', $post );1472 $this->assertEquals( 'raw', $post->filter );1473 }1474 $posts = wp_list_pluck( $query->posts, 'ID' );1475 $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts );1476 }1477 1478 /**1479 * @ticket 168291480 * @group meta1481 */1482 function test_meta_default_compare() {1483 // compare should default to IN when meta_value is an array1484 $post_id = $this->factory->post->create();1485 add_post_meta( $post_id, 'foo', 'bar' );1486 $post_id2 = $this->factory->post->create();1487 add_post_meta( $post_id2, 'bar', 'baz' );1488 $post_id3 = $this->factory->post->create();1489 add_post_meta( $post_id3, 'foo', 'baz' );1490 $post_id4 = $this->factory->post->create();1491 add_post_meta( $post_id4, 'baz', 'bar' );1492 $post_id5 = $this->factory->post->create();1493 add_post_meta( $post_id5, 'foo', rand_str() );1494 1495 $posts = get_posts( array(1496 'meta_key' => 'foo',1497 'meta_value' => array( 'bar', 'baz' )1498 ) );1499 1500 $this->assertEquals( 2, count( $posts ) );1501 $posts = wp_list_pluck( $posts, 'ID' );1502 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );1503 1504 $posts = get_posts( array(1505 'meta_key' => 'foo',1506 'meta_value' => array( 'bar', 'baz' ),1507 'meta_compare' => 'IN'1508 ) );1509 1510 $this->assertEquals( 2, count( $posts ) );1511 foreach ( $posts as $post ) {1512 $this->assertInstanceOf( 'WP_Post', $post );1513 $this->assertEquals( 'raw', $post->filter );1514 }1515 $posts = wp_list_pluck( $posts, 'ID' );1516 $this->assertEqualSets( array( $post_id, $post_id3 ), $posts );1517 }1518 1519 /**1520 * @ticket 172641521 * @group meta1522 */1523 function test_duplicate_posts_when_no_key() {1524 $post_id = $this->factory->post->create();1525 add_post_meta( $post_id, 'city', 'Lorem' );1526 add_post_meta( $post_id, 'address', '123 Lorem St.' );1527 $post_id2 = $this->factory->post->create();1528 add_post_meta( $post_id2, 'city', 'Lorem' );1529 $post_id3 = $this->factory->post->create();1530 add_post_meta( $post_id3, 'city', 'Loren' );1531 1532 $args = array(1533 'meta_query' => array(1534 array(1535 'value' => 'lorem',1536 'compare' => 'LIKE'1537 )1538 )1539 );1540 1541 $posts = get_posts( $args );1542 $this->assertEquals( 2, count( $posts ) );1543 foreach ( $posts as $post ) {1544 $this->assertInstanceOf( 'WP_Post', $post );1545 $this->assertEquals( 'raw', $post->filter );1546 }1547 $posts = wp_list_pluck( $posts, 'ID' );1548 $this->assertEqualSets( array( $post_id, $post_id2 ), $posts );1549 }1550 1551 /**1552 * @ticket 152921553 * @group meta1554 */1555 function test_empty_meta_value() {1556 $post_id = $this->factory->post->create();1557 add_post_meta( $post_id, 'foo', '0' );1558 add_post_meta( $post_id, 'bar', 0 );1559 $post_id2 = $this->factory->post->create();1560 add_post_meta( $post_id2, 'foo', 1 );1561 $post_id3 = $this->factory->post->create();1562 add_post_meta( $post_id3, 'baz', 0 );1563 $post_id4 = $this->factory->post->create();1564 add_post_meta( $post_id4, 'baz', 0 );1565 $post_id5 = $this->factory->post->create();1566 add_post_meta( $post_id5, 'baz', 0 );1567 add_post_meta( $post_id5, 'bar', '0' );1568 $post_id6 = $this->factory->post->create();1569 add_post_meta( $post_id6, 'baz', 0 );1570 1571 $q = new WP_Query( array( 'meta_key' => 'foo', 'meta_value' => '0' ) );1572 $this->assertEquals( 1, count ( $q->posts ) );1573 foreach ( $q->posts as $post ) {1574 $this->assertInstanceOf( 'WP_Post', $post );1575 $this->assertEquals( 'raw', $post->filter );1576 }1577 $this->assertEquals( $post_id, $q->posts[0]->ID );1578 1579 $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) );1580 $this->assertEquals( 2, count ( $posts ) );1581 foreach ( $posts as $post ) {1582 $this->assertInstanceOf( 'WP_Post', $post );1583 $this->assertEquals( 'raw', $post->filter );1584 }1585 $posts = wp_list_pluck( $posts, 'ID' );1586 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );1587 1588 $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => 0 ) );1589 $this->assertEquals( 2, count ( $posts ) );1590 foreach ( $posts as $post ) {1591 $this->assertInstanceOf( 'WP_Post', $post );1592 $this->assertEquals( 'raw', $post->filter );1593 }1594 $posts = wp_list_pluck( $posts, 'ID' );1595 $this->assertEqualSets( array( $post_id, $post_id5 ), $posts );1596 1597 $posts = get_posts( array( 'meta_value' => 0 ) );1598 $this->assertEquals( 5, count ( $posts ) );1599 foreach ( $posts as $post ) {1600 $this->assertInstanceOf( 'WP_Post', $post );1601 $this->assertEquals( 'raw', $post->filter );1602 }1603 $posts = wp_list_pluck( $posts, 'ID' );1604 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );1605 1606 $posts = get_posts( array( 'meta_value' => '0' ) );1607 $this->assertEquals( 5, count ( $posts ) );1608 foreach ( $posts as $post ) {1609 $this->assertInstanceOf( 'WP_Post', $post );1610 $this->assertEquals( 'raw', $post->filter );1611 }1612 $posts = wp_list_pluck( $posts, 'ID' );1613 $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts );1614 }1615 1616 /**1617 * @group taxonomy1618 */1619 public function test_tax_query_single_query_single_term_field_slug() {1620 $t = $this->factory->term->create( array(1621 'taxonomy' => 'category',1622 'slug' => 'foo',1623 'name' => 'Foo',1624 ) );1625 $p1 = $this->factory->post->create();1626 $p2 = $this->factory->post->create();1627 1628 wp_set_post_terms( $p1, $t, 'category' );1629 1630 $q = new WP_Query( array(1631 'fields' => 'ids',1632 'update_post_meta_cache' => false,1633 'update_post_term_cache' => false,1634 'tax_query' => array(1635 array(1636 'taxonomy' => 'category',1637 'terms' => array( 'foo' ),1638 'field' => 'slug',1639 ),1640 ),1641 ) );1642 1643 $this->assertEquals( array( $p1 ), $q->posts );1644 }1645 1646 /**1647 * @group taxonomy1648 */1649 public function test_tax_query_single_query_single_term_field_name() {1650 $t = $this->factory->term->create( array(1651 'taxonomy' => 'category',1652 'slug' => 'foo',1653 'name' => 'Foo',1654 ) );1655 $p1 = $this->factory->post->create();1656 $p2 = $this->factory->post->create();1657 1658 wp_set_post_terms( $p1, $t, 'category' );1659 1660 $q = new WP_Query( array(1661 'fields' => 'ids',1662 'update_post_meta_cache' => false,1663 'update_post_term_cache' => false,1664 'tax_query' => array(1665 array(1666 'taxonomy' => 'category',1667 'terms' => array( 'Foo' ),1668 'field' => 'name',1669 ),1670 ),1671 ) );1672 1673 $this->assertEquals( array( $p1 ), $q->posts );1674 }1675 1676 /**1677 * @group taxonomy1678 */1679 public function test_tax_query_single_query_single_term_field_term_taxonomy_id() {1680 $t = $this->factory->term->create( array(1681 'taxonomy' => 'category',1682 'slug' => 'foo',1683 'name' => 'Foo',1684 ) );1685 $p1 = $this->factory->post->create();1686 $p2 = $this->factory->post->create();1687 1688 $tt_ids = wp_set_post_terms( $p1, $t, 'category' );1689 1690 $q = new WP_Query( array(1691 'fields' => 'ids',1692 'update_post_meta_cache' => false,1693 'update_post_term_cache' => false,1694 'tax_query' => array(1695 array(1696 'taxonomy' => 'category',1697 'terms' => $tt_ids,1698 'field' => 'term_taxonomy_id',1699 ),1700 ),1701 ) );1702 1703 $this->assertEquals( array( $p1 ), $q->posts );1704 }1705 1706 /**1707 * @group taxonomy1708 */1709 public function test_tax_query_single_query_single_term_field_term_id() {1710 $t = $this->factory->term->create( array(1711 'taxonomy' => 'category',1712 'slug' => 'foo',1713 'name' => 'Foo',1714 ) );1715 $p1 = $this->factory->post->create();1716 $p2 = $this->factory->post->create();1717 1718 wp_set_post_terms( $p1, $t, 'category' );1719 1720 $q = new WP_Query( array(1721 'fields' => 'ids',1722 'update_post_meta_cache' => false,1723 'update_post_term_cache' => false,1724 'tax_query' => array(1725 array(1726 'taxonomy' => 'category',1727 'terms' => array( $t ),1728 'field' => 'term_id',1729 ),1730 ),1731 ) );1732 1733 $this->assertEquals( array( $p1 ), $q->posts );1734 }1735 1736 /**1737 * @group taxonomy1738 */1739 public function test_tax_query_single_query_single_term_operator_in() {1740 $t = $this->factory->term->create( array(1741 'taxonomy' => 'category',1742 'slug' => 'foo',1743 'name' => 'Foo',1744 ) );1745 $p1 = $this->factory->post->create();1746 $p2 = $this->factory->post->create();1747 1748 wp_set_post_terms( $p1, $t, 'category' );1749 1750 $q = new WP_Query( array(1751 'fields' => 'ids',1752 'update_post_meta_cache' => false,1753 'update_post_term_cache' => false,1754 'tax_query' => array(1755 array(1756 'taxonomy' => 'category',1757 'terms' => array( 'foo' ),1758 'field' => 'slug',1759 'operator' => 'IN',1760 ),1761 ),1762 ) );1763 1764 $this->assertEquals( array( $p1 ), $q->posts );1765 }1766 1767 /**1768 * @group taxonomy1769 */1770 public function test_tax_query_single_query_single_term_operator_not_in() {1771 $t = $this->factory->term->create( array(1772 'taxonomy' => 'category',1773 'slug' => 'foo',1774 'name' => 'Foo',1775 ) );1776 $p1 = $this->factory->post->create();1777 $p2 = $this->factory->post->create();1778 1779 wp_set_post_terms( $p1, $t, 'category' );1780 1781 $q = new WP_Query( array(1782 'fields' => 'ids',1783 'update_post_meta_cache' => false,1784 'update_post_term_cache' => false,1785 'tax_query' => array(1786 array(1787 'taxonomy' => 'category',1788 'terms' => array( 'foo' ),1789 'field' => 'slug',1790 'operator' => 'NOT IN',1791 ),1792 ),1793 ) );1794 1795 $this->assertEquals( array( $p2 ), $q->posts );1796 }1797 1798 /**1799 * @group taxonomy1800 */1801 public function test_tax_query_single_query_single_term_operator_and() {1802 $t = $this->factory->term->create( array(1803 'taxonomy' => 'category',1804 'slug' => 'foo',1805 'name' => 'Foo',1806 ) );1807 $p1 = $this->factory->post->create();1808 $p2 = $this->factory->post->create();1809 1810 wp_set_post_terms( $p1, $t, 'category' );1811 1812 $q = new WP_Query( array(1813 'fields' => 'ids',1814 'update_post_meta_cache' => false,1815 'update_post_term_cache' => false,1816 'tax_query' => array(1817 array(1818 'taxonomy' => 'category',1819 'terms' => array( 'foo' ),1820 'field' => 'slug',1821 'operator' => 'AND',1822 ),1823 ),1824 ) );1825 1826 $this->assertEquals( array( $p1 ), $q->posts );1827 }1828 1829 /**1830 * @group taxonomy1831 */1832 public function test_tax_query_single_query_multiple_terms_operator_in() {1833 $t1 = $this->factory->term->create( array(1834 'taxonomy' => 'category',1835 'slug' => 'foo',1836 'name' => 'Foo',1837 ) );1838 $t2 = $this->factory->term->create( array(1839 'taxonomy' => 'category',1840 'slug' => 'bar',1841 'name' => 'Bar',1842 ) );1843 $p1 = $this->factory->post->create();1844 $p2 = $this->factory->post->create();1845 $p3 = $this->factory->post->create();1846 1847 wp_set_post_terms( $p1, $t1, 'category' );1848 wp_set_post_terms( $p2, $t2, 'category' );1849 1850 $q = new WP_Query( array(1851 'fields' => 'ids',1852 'update_post_meta_cache' => false,1853 'update_post_term_cache' => false,1854 'tax_query' => array(1855 array(1856 'taxonomy' => 'category',1857 'terms' => array( 'foo', 'bar' ),1858 'field' => 'slug',1859 'operator' => 'IN',1860 ),1861 ),1862 ) );1863 1864 $this->assertEqualSets( array( $p1, $p2 ), $q->posts );1865 }1866 1867 /**1868 * @group taxonomy1869 */1870 public function test_tax_query_single_query_multiple_terms_operator_not_in() {1871 $t1 = $this->factory->term->create( array(1872 'taxonomy' => 'category',1873 'slug' => 'foo',1874 'name' => 'Foo',1875 ) );1876 $t2 = $this->factory->term->create( array(1877 'taxonomy' => 'category',1878 'slug' => 'bar',1879 'name' => 'Bar',1880 ) );1881 $p1 = $this->factory->post->create();1882 $p2 = $this->factory->post->create();1883 $p3 = $this->factory->post->create();1884 1885 wp_set_post_terms( $p1, $t1, 'category' );1886 wp_set_post_terms( $p2, $t2, 'category' );1887 1888 $q = new WP_Query( array(1889 'fields' => 'ids',1890 'update_post_meta_cache' => false,1891 'update_post_term_cache' => false,1892 'tax_query' => array(1893 array(1894 'taxonomy' => 'category',1895 'terms' => array( 'foo', 'bar' ),1896 'field' => 'slug',1897 'operator' => 'NOT IN',1898 ),1899 ),1900 ) );1901 1902 $this->assertEquals( array( $p3 ), $q->posts );1903 }1904 1905 /**1906 * @group taxonomy1907 * @ticket 181051908 */1909 public function test_tax_query_single_query_multiple_queries_operator_not_in() {1910 $t1 = $this->factory->term->create( array(1911 'taxonomy' => 'category',1912 'slug' => 'foo',1913 'name' => 'Foo',1914 ) );1915 $t2 = $this->factory->term->create( array(1916 'taxonomy' => 'category',1917 'slug' => 'bar',1918 'name' => 'Bar',1919 ) );1920 $p1 = $this->factory->post->create();1921 $p2 = $this->factory->post->create();1922 $p3 = $this->factory->post->create();1923 1924 wp_set_post_terms( $p1, $t1, 'category' );1925 wp_set_post_terms( $p2, $t2, 'category' );1926 1927 $q = new WP_Query( array(1928 'fields' => 'ids',1929 'update_post_meta_cache' => false,1930 'update_post_term_cache' => false,1931 'tax_query' => array(1932 'relation' => 'AND',1933 array(1934 'taxonomy' => 'category',1935 'terms' => array( 'foo' ),1936 'field' => 'slug',1937 'operator' => 'NOT IN',1938 ),1939 array(1940 'taxonomy' => 'category',1941 'terms' => array( 'bar' ),1942 'field' => 'slug',1943 'operator' => 'NOT IN',1944 ),1945 ),1946 ) );1947 1948 $this->assertEquals( array( $p3 ), $q->posts );1949 }1950 1951 /**1952 * @group taxonomy1953 */1954 public function test_tax_query_single_query_multiple_terms_operator_and() {1955 $t1 = $this->factory->term->create( array(1956 'taxonomy' => 'category',1957 'slug' => 'foo',1958 'name' => 'Foo',1959 ) );1960 $t2 = $this->factory->term->create( array(1961 'taxonomy' => 'category',1962 'slug' => 'bar',1963 'name' => 'Bar',1964 ) );1965 $p1 = $this->factory->post->create();1966 $p2 = $this->factory->post->create();1967 $p3 = $this->factory->post->create();1968 1969 wp_set_object_terms( $p1, $t1, 'category' );1970 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );1971 1972 $q = new WP_Query( array(1973 'fields' => 'ids',1974 'update_post_meta_cache' => false,1975 'update_post_term_cache' => false,1976 'tax_query' => array(1977 array(1978 'taxonomy' => 'category',1979 'terms' => array( 'foo', 'bar' ),1980 'field' => 'slug',1981 'operator' => 'AND',1982 ),1983 ),1984 ) );1985 1986 $this->assertEquals( array( $p2 ), $q->posts );1987 }1988 1989 /**1990 * @ticket 291811991 */1992 public function test_tax_query_operator_not_exists() {1993 register_taxonomy( 'wptests_tax1', 'post' );1994 register_taxonomy( 'wptests_tax2', 'post' );1995 1996 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );1997 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );1998 1999 $p1 = $this->factory->post->create();2000 $p2 = $this->factory->post->create();2001 $p3 = $this->factory->post->create();2002 2003 wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );2004 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );2005 2006 $q = new WP_Query( array(2007 'fields' => 'ids',2008 'orderby' => 'ID',2009 'order' => 'ASC',2010 'tax_query' => array(2011 array(2012 'taxonomy' => 'wptests_tax2',2013 'operator' => 'NOT EXISTS',2014 ),2015 ),2016 ) );2017 2018 $this->assertEqualSets( array( $p1, $p3 ), $q->posts );2019 }2020 2021 /**2022 * @ticket 291812023 */2024 public function test_tax_query_operator_exists() {2025 register_taxonomy( 'wptests_tax1', 'post' );2026 register_taxonomy( 'wptests_tax2', 'post' );2027 2028 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );2029 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );2030 2031 $p1 = $this->factory->post->create();2032 $p2 = $this->factory->post->create();2033 $p3 = $this->factory->post->create();2034 2035 wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );2036 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );2037 2038 $q = new WP_Query( array(2039 'fields' => 'ids',2040 'orderby' => 'ID',2041 'order' => 'ASC',2042 'tax_query' => array(2043 array(2044 'taxonomy' => 'wptests_tax2',2045 'operator' => 'EXISTS',2046 ),2047 ),2048 ) );2049 2050 $this->assertEqualSets( array( $p2 ), $q->posts );2051 }2052 2053 /**2054 * @ticket 291812055 */2056 public function test_tax_query_operator_exists_should_ignore_terms() {2057 register_taxonomy( 'wptests_tax1', 'post' );2058 register_taxonomy( 'wptests_tax2', 'post' );2059 2060 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );2061 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );2062 2063 $p1 = $this->factory->post->create();2064 $p2 = $this->factory->post->create();2065 $p3 = $this->factory->post->create();2066 2067 wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );2068 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );2069 2070 $q = new WP_Query( array(2071 'fields' => 'ids',2072 'orderby' => 'ID',2073 'order' => 'ASC',2074 'tax_query' => array(2075 array(2076 'taxonomy' => 'wptests_tax2',2077 'operator' => 'EXISTS',2078 'terms' => array( 'foo', 'bar' ),2079 ),2080 ),2081 ) );2082 2083 $this->assertEqualSets( array( $p2 ), $q->posts );2084 }2085 2086 /**2087 * @ticket 291812088 */2089 public function test_tax_query_operator_exists_with_no_taxonomy() {2090 register_taxonomy( 'wptests_tax1', 'post' );2091 register_taxonomy( 'wptests_tax2', 'post' );2092 2093 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) );2094 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) );2095 2096 $p1 = $this->factory->post->create();2097 $p2 = $this->factory->post->create();2098 $p3 = $this->factory->post->create();2099 2100 wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );2101 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );2102 2103 $q = new WP_Query( array(2104 'fields' => 'ids',2105 'orderby' => 'ID',2106 'order' => 'ASC',2107 'tax_query' => array(2108 array(2109 'operator' => 'EXISTS',2110 ),2111 ),2112 ) );2113 2114 $this->assertEmpty( $q->posts );2115 }2116 2117 /**2118 * @group taxonomy2119 */2120 public function test_tax_query_multiple_queries_relation_and() {2121 $t1 = $this->factory->term->create( array(2122 'taxonomy' => 'category',2123 'slug' => 'foo',2124 'name' => 'Foo',2125 ) );2126 $t2 = $this->factory->term->create( array(2127 'taxonomy' => 'category',2128 'slug' => 'bar',2129 'name' => 'Bar',2130 ) );2131 $p1 = $this->factory->post->create();2132 $p2 = $this->factory->post->create();2133 $p3 = $this->factory->post->create();2134 2135 wp_set_object_terms( $p1, $t1, 'category' );2136 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );2137 2138 $q = new WP_Query( array(2139 'fields' => 'ids',2140 'update_post_meta_cache' => false,2141 'update_post_term_cache' => false,2142 'tax_query' => array(2143 'relation' => 'AND',2144 array(2145 'taxonomy' => 'category',2146 'terms' => array( 'foo' ),2147 'field' => 'slug',2148 ),2149 array(2150 'taxonomy' => 'category',2151 'terms' => array( 'bar' ),2152 'field' => 'slug',2153 ),2154 ),2155 ) );2156 2157 $this->assertEquals( array( $p2 ), $q->posts );2158 }2159 2160 /**2161 * @group taxonomy2162 */2163 public function test_tax_query_multiple_queries_relation_or() {2164 $t1 = $this->factory->term->create( array(2165 'taxonomy' => 'category',2166 'slug' => 'foo',2167 'name' => 'Foo',2168 ) );2169 $t2 = $this->factory->term->create( array(2170 'taxonomy' => 'category',2171 'slug' => 'bar',2172 'name' => 'Bar',2173 ) );2174 $p1 = $this->factory->post->create();2175 $p2 = $this->factory->post->create();2176 $p3 = $this->factory->post->create();2177 2178 wp_set_object_terms( $p1, $t1, 'category' );2179 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );2180 2181 $q = new WP_Query( array(2182 'fields' => 'ids',2183 'update_post_meta_cache' => false,2184 'update_post_term_cache' => false,2185 'tax_query' => array(2186 'relation' => 'OR',2187 array(2188 'taxonomy' => 'category',2189 'terms' => array( 'foo' ),2190 'field' => 'slug',2191 ),2192 array(2193 'taxonomy' => 'category',2194 'terms' => array( 'bar' ),2195 'field' => 'slug',2196 ),2197 ),2198 ) );2199 2200 $this->assertEqualSets( array( $p1, $p2 ), $q->posts );2201 }2202 2203 /**2204 * @group taxonomy2205 */2206 public function test_tax_query_multiple_queries_different_taxonomies() {2207 $t1 = $this->factory->term->create( array(2208 'taxonomy' => 'post_tag',2209 'slug' => 'foo',2210 'name' => 'Foo',2211 ) );2212 $t2 = $this->factory->term->create( array(2213 'taxonomy' => 'category',2214 'slug' => 'bar',2215 'name' => 'Bar',2216 ) );2217 $p1 = $this->factory->post->create();2218 $p2 = $this->factory->post->create();2219 $p3 = $this->factory->post->create();2220 2221 wp_set_object_terms( $p1, $t1, 'post_tag' );2222 wp_set_object_terms( $p2, $t2, 'category' );2223 2224 $q = new WP_Query( array(2225 'fields' => 'ids',2226 'update_post_meta_cache' => false,2227 'update_post_term_cache' => false,2228 'tax_query' => array(2229 'relation' => 'OR',2230 array(2231 'taxonomy' => 'post_tag',2232 'terms' => array( 'foo' ),2233 'field' => 'slug',2234 ),2235 array(2236 'taxonomy' => 'category',2237 'terms' => array( 'bar' ),2238 'field' => 'slug',2239 ),2240 ),2241 ) );2242 2243 $this->assertEqualSets( array( $p1, $p2 ), $q->posts );2244 }2245 2246 /**2247 * @ticket 297382248 * @group taxonomy2249 */2250 public function test_tax_query_two_nested_queries() {2251 register_taxonomy( 'foo', 'post' );2252 register_taxonomy( 'bar', 'post' );2253 2254 $foo_term_1 = $this->factory->term->create( array(2255 'taxonomy' => 'foo',2256 ) );2257 $foo_term_2 = $this->factory->term->create( array(2258 'taxonomy' => 'foo',2259 ) );2260 $bar_term_1 = $this->factory->term->create( array(2261 'taxonomy' => 'bar',2262 ) );2263 $bar_term_2 = $this->factory->term->create( array(2264 'taxonomy' => 'bar',2265 ) );2266 2267 $p1 = $this->factory->post->create();2268 $p2 = $this->factory->post->create();2269 $p3 = $this->factory->post->create();2270 2271 wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );2272 wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );2273 wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );2274 wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );2275 wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );2276 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );2277 2278 $q = new WP_Query( array(2279 'fields' => 'ids',2280 'update_post_meta_cache' => false,2281 'update_post_term_cache' => false,2282 'tax_query' => array(2283 'relation' => 'OR',2284 array(2285 'relation' => 'AND',2286 array(2287 'taxonomy' => 'foo',2288 'terms' => array( $foo_term_1 ),2289 'field' => 'term_id',2290 ),2291 array(2292 'taxonomy' => 'bar',2293 'terms' => array( $bar_term_1 ),2294 'field' => 'term_id',2295 ),2296 ),2297 array(2298 'relation' => 'AND',2299 array(2300 'taxonomy' => 'foo',2301 'terms' => array( $foo_term_2 ),2302 'field' => 'term_id',2303 ),2304 array(2305 'taxonomy' => 'bar',2306 'terms' => array( $bar_term_2 ),2307 'field' => 'term_id',2308 ),2309 ),2310 ),2311 ) );2312 2313 _unregister_taxonomy( 'foo' );2314 _unregister_taxonomy( 'bar' );2315 2316 $this->assertEqualSets( array( $p1, $p2 ), $q->posts );2317 }2318 2319 /**2320 * @ticket 297382321 * @group taxonomy2322 */2323 public function test_tax_query_one_nested_query_one_first_order_query() {2324 register_taxonomy( 'foo', 'post' );2325 register_taxonomy( 'bar', 'post' );2326 2327 $foo_term_1 = $this->factory->term->create( array(2328 'taxonomy' => 'foo',2329 ) );2330 $foo_term_2 = $this->factory->term->create( array(2331 'taxonomy' => 'foo',2332 ) );2333 $bar_term_1 = $this->factory->term->create( array(2334 'taxonomy' => 'bar',2335 ) );2336 $bar_term_2 = $this->factory->term->create( array(2337 'taxonomy' => 'bar',2338 ) );2339 2340 $p1 = $this->factory->post->create();2341 $p2 = $this->factory->post->create();2342 $p3 = $this->factory->post->create();2343 2344 wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );2345 wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );2346 wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );2347 wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );2348 wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );2349 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );2350 2351 $q = new WP_Query( array(2352 'fields' => 'ids',2353 'update_post_meta_cache' => false,2354 'update_post_term_cache' => false,2355 'tax_query' => array(2356 'relation' => 'OR',2357 array(2358 'taxonomy' => 'foo',2359 'terms' => array( $foo_term_2 ),2360 'field' => 'term_id',2361 ),2362 array(2363 'relation' => 'AND',2364 array(2365 'taxonomy' => 'foo',2366 'terms' => array( $foo_term_1 ),2367 'field' => 'term_id',2368 ),2369 array(2370 'taxonomy' => 'bar',2371 'terms' => array( $bar_term_1 ),2372 'field' => 'term_id',2373 ),2374 ),2375 ),2376 ) );2377 2378 _unregister_taxonomy( 'foo' );2379 _unregister_taxonomy( 'bar' );2380 2381 $this->assertEqualSets( array( $p1, $p2 ), $q->posts );2382 }2383 2384 /**2385 * @ticket 297382386 * @group taxonomy2387 */2388 public function test_tax_query_one_double_nested_query_one_first_order_query() {2389 register_taxonomy( 'foo', 'post' );2390 register_taxonomy( 'bar', 'post' );2391 2392 $foo_term_1 = $this->factory->term->create( array(2393 'taxonomy' => 'foo',2394 ) );2395 $foo_term_2 = $this->factory->term->create( array(2396 'taxonomy' => 'foo',2397 ) );2398 $bar_term_1 = $this->factory->term->create( array(2399 'taxonomy' => 'bar',2400 ) );2401 $bar_term_2 = $this->factory->term->create( array(2402 'taxonomy' => 'bar',2403 ) );2404 2405 $p1 = $this->factory->post->create();2406 $p2 = $this->factory->post->create();2407 $p3 = $this->factory->post->create();2408 $p4 = $this->factory->post->create();2409 2410 wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );2411 wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );2412 wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );2413 wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );2414 wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );2415 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );2416 2417 $q = new WP_Query( array(2418 'fields' => 'ids',2419 'update_post_meta_cache' => false,2420 'update_post_term_cache' => false,2421 'tax_query' => array(2422 'relation' => 'OR',2423 array(2424 'taxonomy' => 'foo',2425 'terms' => array( $foo_term_2 ),2426 'field' => 'term_id',2427 ),2428 array(2429 'relation' => 'AND',2430 array(2431 'taxonomy' => 'foo',2432 'terms' => array( $foo_term_1 ),2433 'field' => 'term_id',2434 ),2435 array(2436 'relation' => 'OR',2437 array(2438 'taxonomy' => 'bar',2439 'terms' => array( $bar_term_1 ),2440 'field' => 'term_id',2441 ),2442 array(2443 'taxonomy' => 'bar',2444 'terms' => array( $bar_term_2 ),2445 'field' => 'term_id',2446 ),2447 ),2448 ),2449 ),2450 ) );2451 2452 _unregister_taxonomy( 'foo' );2453 _unregister_taxonomy( 'bar' );2454 2455 $this->assertEqualSets( array( $p1, $p2, $p3 ), $q->posts );2456 }2457 2458 /**2459 * @ticket 206042460 * @group taxonomy2461 */2462 public function test_tax_query_relation_or_both_clauses_empty_terms() {2463 // An empty tax query should return an empty array, not all posts.2464 2465 $this->factory->post->create_many( 10 );2466 2467 $query = new WP_Query( array(2468 'fields' => 'ids',2469 'update_post_term_cache' => false,2470 'update_post_meta_cache' => false,2471 'tax_query' => array(2472 'relation' => 'OR',2473 array(2474 'taxonomy' => 'post_tag',2475 'field' => 'id',2476 'terms' => false,2477 'operator' => 'IN'2478 ),2479 array(2480 'taxonomy' => 'category',2481 'field' => 'id',2482 'terms' => false,2483 'operator' => 'IN'2484 ),2485 )2486 ) );2487 2488 $posts = $query->get_posts();2489 $this->assertEquals( 0 , count( $posts ) );2490 }2491 2492 /**2493 * @ticket 206042494 * @group taxonomy2495 */2496 public function test_tax_query_relation_or_one_clause_empty_terms() {2497 // An empty tax query should return an empty array, not all posts.2498 2499 $this->factory->post->create_many( 10 );2500 2501 $query = new WP_Query( array(2502 'fields' => 'ids',2503 'update_post_term_cache' => false,2504 'update_post_meta_cache' => false,2505 'tax_query' => array(2506 'relation' => 'OR',2507 array(2508 'taxonomy' => 'post_tag',2509 'field' => 'id',2510 'terms' => array( 'foo' ),2511 'operator' => 'IN'2512 ),2513 array(2514 'taxonomy' => 'category',2515 'field' => 'id',2516 'terms' => false,2517 'operator' => 'IN'2518 ),2519 )2520 ) );2521 2522 $posts = $query->get_posts();2523 $this->assertEquals( 0 , count( $posts ) );2524 }2525 2526 /**2527 * @group taxonomy2528 */2529 public function test_tax_query_include_children() {2530 $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) );2531 $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) );2532 $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'East Syndney', 'parent' => $cat_b ) );2533 $cat_d = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'West Syndney', 'parent' => $cat_b ) );2534 2535 $post_a = $this->factory->post->create( array( 'post_category' => array( $cat_a ) ) );2536 $post_b = $this->factory->post->create( array( 'post_category' => array( $cat_b ) ) );2537 $post_c = $this->factory->post->create( array( 'post_category' => array( $cat_c ) ) );2538 $post_d = $this->factory->post->create( array( 'post_category' => array( $cat_d ) ) );2539 2540 $posts = get_posts( array(2541 'fields' => 'ids',2542 'update_post_meta_cache' => false,2543 'update_post_term_cache' => false,2544 'tax_query' => array(2545 array(2546 'taxonomy' => 'category',2547 'field' => 'id',2548 'terms' => array( $cat_a ),2549 )2550 )2551 ) );2552 2553 $this->assertEquals( 4 , count( $posts ) );2554 2555 $posts = get_posts( array(2556 'fields' => 'ids',2557 'update_post_meta_cache' => false,2558 'update_post_term_cache' => false,2559 'tax_query' => array(2560 array(2561 'taxonomy' => 'category',2562 'field' => 'id',2563 'terms' => array( $cat_a ),2564 'include_children' => false2565 )2566 )2567 ) );2568 2569 $this->assertEquals( 1 , count( $posts ) );2570 2571 $posts = get_posts( array(2572 'fields' => 'ids',2573 'update_post_meta_cache' => false,2574 'update_post_term_cache' => false,2575 'tax_query' => array(2576 array(2577 'taxonomy' => 'category',2578 'field' => 'id',2579 'terms' => array( $cat_b ),2580 )2581 )2582 ) );2583 2584 $this->assertEquals( 3 , count( $posts ) );2585 2586 $posts = get_posts( array(2587 'fields' => 'ids',2588 'update_post_meta_cache' => false,2589 'update_post_term_cache' => false,2590 'tax_query' => array(2591 array(2592 'taxonomy' => 'category',2593 'field' => 'id',2594 'terms' => array( $cat_b ),2595 'include_children' => false2596 )2597 )2598 ) );2599 2600 $this->assertEquals( 1 , count( $posts ) );2601 2602 $posts = get_posts( array(2603 'fields' => 'ids',2604 'update_post_meta_cache' => false,2605 'update_post_term_cache' => false,2606 'tax_query' => array(2607 array(2608 'taxonomy' => 'category',2609 'field' => 'id',2610 'terms' => array( $cat_c ),2611 )2612 )2613 ) );2614 2615 $this->assertEquals( 1 , count( $posts ) );2616 2617 $posts = get_posts( array(2618 'fields' => 'ids',2619 'update_post_meta_cache' => false,2620 'update_post_term_cache' => false,2621 'tax_query' => array(2622 array(2623 'taxonomy' => 'category',2624 'field' => 'id',2625 'terms' => array( $cat_c ),2626 'include_children' => false2627 )2628 )2629 ) );2630 2631 $this->assertEquals( 1 , count( $posts ) );2632 6 } 2633 7 … … 2664 38 2665 39 /** 2666 * @group taxonomy2667 */2668 public function test_tax_query_taxonomy_with_attachments() {2669 $q = new WP_Query();2670 2671 register_taxonomy_for_object_type( 'post_tag', 'attachment:image' );2672 $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) );2673 $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(2674 'post_mime_type' => 'image/jpeg',2675 'post_type' => 'attachment'2676 ) );2677 wp_set_object_terms( $image_id, $tag_id, 'post_tag' );2678 2679 $posts = $q->query( array(2680 'fields' => 'ids',2681 'update_post_meta_cache' => false,2682 'update_post_term_cache' => false,2683 'post_type' => 'attachment',2684 'post_status' => 'inherit',2685 'tax_query' => array(2686 array(2687 'taxonomy' => 'post_tag',2688 'field' => 'term_id',2689 'terms' => array( $tag_id )2690 )2691 )2692 ) );2693 2694 $this->assertEquals( array( $image_id ), $posts );2695 }2696 2697 /**2698 * @group taxonomy2699 */2700 function test_tax_query_no_taxonomy() {2701 $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) );2702 $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) );2703 2704 $response1 = new WP_Query( array(2705 'tax_query' => array(2706 array( 'terms' => array( $cat_id ) )2707 )2708 ) );2709 $this->assertEmpty( $response1->posts );2710 2711 $response2 = new WP_Query( array(2712 'fields' => 'ids',2713 'update_post_meta_cache' => false,2714 'update_post_term_cache' => false,2715 'tax_query' => array(2716 array(2717 'taxonomy' => 'category',2718 'terms' => array( $cat_id )2719 )2720 )2721 ) );2722 $this->assertNotEmpty( $response2->posts );2723 2724 $term = get_category( $cat_id );2725 $response3 = new WP_Query( array(2726 'fields' => 'ids',2727 'update_post_meta_cache' => false,2728 'update_post_term_cache' => false,2729 'tax_query' => array(2730 array(2731 'field' => 'term_taxonomy_id',2732 'terms' => array( $term->term_taxonomy_id )2733 )2734 )2735 ) );2736 $this->assertNotEmpty( $response3->posts );2737 }2738 2739 /**2740 * @group taxonomy2741 */2742 function test_term_taxonomy_id_field_no_taxonomy() {2743 $q = new WP_Query();2744 2745 $posts = $this->factory->post->create_many( 5 );2746 2747 $cats = $tags = array();2748 2749 // need term_taxonomy_ids in addition to term_ids, so no factory2750 for ( $i = 0; $i < 5; $i++ ) {2751 $cats[$i] = wp_insert_term( 'category-' . $i , 'category' );2752 $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' );2753 2754 // post 0 gets all terms2755 wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true );2756 wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true );2757 }2758 2759 wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );2760 wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );2761 2762 wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );2763 wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );2764 2765 wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );2766 wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );2767 2768 $results1 = $q->query( array(2769 'fields' => 'ids',2770 'update_post_meta_cache' => false,2771 'update_post_term_cache' => false,2772 'orderby' => 'ID',2773 'order' => 'ASC',2774 'tax_query' => array(2775 'relation' => 'OR',2776 array(2777 'field' => 'term_taxonomy_id',2778 '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'] ),2779 'operator' => 'AND',2780 'include_children' => false,2781 ),2782 array(2783 'field' => 'term_taxonomy_id',2784 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),2785 'operator' => 'AND',2786 'include_children' => false,2787 )2788 )2789 ) );2790 2791 $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' );2792 2793 $results2 = $q->query( array(2794 'fields' => 'ids',2795 'update_post_meta_cache' => false,2796 'update_post_term_cache' => false,2797 'orderby' => 'ID',2798 'order' => 'ASC',2799 'tax_query' => array(2800 'relation' => 'AND',2801 array(2802 'field' => 'term_taxonomy_id',2803 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),2804 'operator' => 'IN',2805 'include_children' => false,2806 ),2807 array(2808 'field' => 'term_taxonomy_id',2809 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),2810 'operator' => 'IN',2811 'include_children' => false,2812 )2813 )2814 ) );2815 2816 $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );2817 }2818 2819 /**2820 40 * @ticket 28099 2821 41 * @group taxonomy … … 2845 65 $q6 = get_posts( array( 'tag_slug__in' => array() ) ); 2846 66 $this->assertNotEmpty( $q6 ); 2847 }2848 2849 /**2850 * @group taxonomy2851 * @ticket 297382852 */2853 public function test_populate_taxonomy_query_var_from_tax_query() {2854 register_taxonomy( 'foo', 'post' );2855 $t = $this->factory->term->create( array(2856 'taxonomy' => 'foo',2857 ) );2858 $c = $this->factory->term->create( array(2859 'taxonomy' => 'category',2860 ) );2861 2862 $q = new WP_Query( array(2863 'tax_query' => array(2864 // Empty terms mean that this one should be skipped2865 array(2866 'taxonomy' => 'bar',2867 'terms' => array(),2868 ),2869 2870 // Category and post tags should be skipped2871 array(2872 'taxonomy' => 'category',2873 'terms' => array( $c ),2874 ),2875 2876 array(2877 'taxonomy' => 'foo',2878 'terms' => array( $t ),2879 ),2880 ),2881 ) );2882 2883 $this->assertSame( 'foo', $q->get( 'taxonomy' ) );2884 2885 _unregister_taxonomy( 'foo' );2886 }2887 2888 /**2889 * @group taxonomy2890 */2891 public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() {2892 register_taxonomy( 'foo', 'post' );2893 register_taxonomy( 'foo1', 'post' );2894 $t = $this->factory->term->create( array(2895 'taxonomy' => 'foo',2896 ) );2897 2898 $q = new WP_Query( array(2899 'taxonomy' => 'bar',2900 'tax_query' => array(2901 array(2902 'taxonomy' => 'foo',2903 'terms' => array( $t ),2904 ),2905 ),2906 ) );2907 2908 $this->assertSame( 'bar', $q->get( 'taxonomy' ) );2909 2910 _unregister_taxonomy( 'foo' );2911 _unregister_taxonomy( 'foo1' );2912 }2913 2914 /**2915 * @group taxonomy2916 */2917 public function test_populate_term_query_var_from_tax_query() {2918 register_taxonomy( 'foo', 'post' );2919 $t = $this->factory->term->create( array(2920 'taxonomy' => 'foo',2921 'slug' => 'bar',2922 ) );2923 2924 $q = new WP_Query( array(2925 'tax_query' => array(2926 array(2927 'taxonomy' => 'foo',2928 'terms' => array( 'bar' ),2929 'field' => 'slug',2930 ),2931 ),2932 ) );2933 2934 $this->assertSame( 'bar', $q->get( 'term' ) );2935 2936 _unregister_taxonomy( 'foo' );2937 }2938 2939 /**2940 * @group taxonomy2941 */2942 public function test_populate_term_id_query_var_from_tax_query() {2943 register_taxonomy( 'foo', 'post' );2944 $t = $this->factory->term->create( array(2945 'taxonomy' => 'foo',2946 'slug' => 'bar',2947 ) );2948 2949 $q = new WP_Query( array(2950 'tax_query' => array(2951 array(2952 'taxonomy' => 'foo',2953 'terms' => array( $t ),2954 'field' => 'term_id',2955 ),2956 ),2957 ) );2958 2959 $this->assertEquals( $t, $q->get( 'term_id' ) );2960 2961 _unregister_taxonomy( 'foo' );2962 }2963 2964 /**2965 * @group taxonomy2966 * @ticket 297382967 */2968 public function test_populate_cat_category_name_query_var_from_tax_query() {2969 register_taxonomy( 'foo', 'post' );2970 $t = $this->factory->term->create( array(2971 'taxonomy' => 'foo',2972 ) );2973 $c = $this->factory->term->create( array(2974 'taxonomy' => 'category',2975 'slug' => 'bar',2976 ) );2977 2978 $q = new WP_Query( array(2979 'tax_query' => array(2980 // Non-category should be skipped2981 array(2982 'taxonomy' => 'foo',2983 'terms' => array( $t ),2984 ),2985 2986 // Empty terms mean that this one should be skipped2987 array(2988 'taxonomy' => 'category',2989 'terms' => array(),2990 ),2991 2992 // Category and post tags should be skipped2993 array(2994 'taxonomy' => 'category',2995 'terms' => array( $c ),2996 ),2997 ),2998 ) );2999 3000 $this->assertEquals( $c, $q->get( 'cat' ) );3001 $this->assertEquals( 'bar', $q->get( 'category_name' ) );3002 3003 _unregister_taxonomy( 'foo' );3004 }3005 3006 /**3007 * @group taxonomy3008 * @ticket 297383009 */3010 public function test_populate_tag_id_query_var_from_tax_query() {3011 register_taxonomy( 'foo', 'post' );3012 $t = $this->factory->term->create( array(3013 'taxonomy' => 'foo',3014 ) );3015 $tag = $this->factory->term->create( array(3016 'taxonomy' => 'post_tag',3017 'slug' => 'bar',3018 ) );3019 3020 $q = new WP_Query( array(3021 'tax_query' => array(3022 // Non-tag should be skipped3023 array(3024 'taxonomy' => 'foo',3025 'terms' => array( $t ),3026 ),3027 3028 // Empty terms mean that this one should be skipped3029 array(3030 'taxonomy' => 'post_tag',3031 'terms' => array(),3032 ),3033 3034 // Category and post tags should be skipped3035 array(3036 'taxonomy' => 'post_tag',3037 'terms' => array( $tag ),3038 ),3039 ),3040 ) );3041 3042 $this->assertEquals( $tag, $q->get( 'tag_id' ) );3043 3044 _unregister_taxonomy( 'foo' );3045 67 } 3046 68 -
trunk/tests/phpunit/tests/query/isTerm.php
r31285 r31286 2 2 3 3 /** 4 * Test the is_*() functions in query.php across the URL structure4 * Test the is_*() functions in query.php related to taxonomy terms across the URL structure. 5 5 * 6 6 * This exercises both query.php and rewrite.php: urls are fed through the rewrite code, … … 9 9 * @group query 10 10 * @group rewrite 11 * @group taxonomy 11 12 */ 12 class Tests_Query_ TaxQueryextends WP_UnitTestCase {13 class Tests_Query_IsTerm extends WP_UnitTestCase { 13 14 protected $tag_id; 14 15 protected $cat_id; -
trunk/tests/phpunit/tests/query/taxQuery.php
r30771 r31286 2 2 3 3 /** 4 * Test the is_*() functions in query.php across the URL structure5 *6 * This exercises both query.php and rewrite.php: urls are fed through the rewrite code,7 * then we test the effects of each url on the wp_query object.8 *9 4 * @group query 10 * @group rewrite5 * @group taxonomy 11 6 */ 12 7 class Tests_Query_TaxQuery extends WP_UnitTestCase { 13 protected $tag_id; 14 protected $cat_id; 15 protected $tax_id; 16 protected $tax_id2; 17 protected $post_id; 18 19 protected $cat; 20 protected $uncat; 21 protected $tag; 22 protected $tax; 23 24 function setUp() { 25 global $wp_rewrite; 26 parent::setUp(); 27 28 set_current_screen( 'front' ); 29 30 $GLOBALS['wp_the_query'] = new WP_Query(); 31 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; 32 33 $wp_rewrite->init(); 34 $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); 35 36 create_initial_taxonomies(); 37 register_taxonomy( 'testtax', 'post', array( 'public' => true ) ); 38 39 $wp_rewrite->flush_rules(); 40 41 $this->tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) ); 42 $this->cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) ); 43 $this->tax_id = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug' ) ); 44 $this->tax_id2 = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug2' ) ); 45 $this->post_id = $this->factory->post->create(); 46 wp_set_object_terms( $this->post_id, $this->cat_id, 'category' ); 47 wp_set_object_terms( $this->post_id, array( $this->tax_id, $this->tax_id2 ), 'testtax' ); 48 49 $this->cat = get_term( $this->cat_id, 'category' ); 50 _make_cat_compat( $this->cat ); 51 $this->tag = get_term( $this->tag_id, 'post_tag' ); 52 53 $this->uncat = get_term_by( 'slug', 'uncategorized', 'category' ); 54 _make_cat_compat( $this->uncat ); 55 56 add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); 57 } 58 59 function tearDown() { 60 global $wp_rewrite; 61 parent::tearDown(); 62 63 _unregister_taxonomy( 'testtax' ); 64 65 $wp_rewrite->init(); 66 67 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); 68 } 69 70 function test_tag_action_tax() { 71 // tag with tax added 72 $this->go_to( home_url( "/tag/tag-slug/" ) ); 73 $this->assertQueryTrue( 'is_tag', 'is_archive' ); 74 $this->assertNotEmpty( get_query_var( 'tax_query' ) ); 75 $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); 76 $this->assertNotEmpty( get_query_var( 'term_id' ) ); 77 $this->assertNotEmpty( get_query_var( 'tag_id' ) ); 78 $this->assertEquals( get_queried_object(), $this->tag ); 79 } 80 81 function test_tag_query_cat_action_tax() { 82 // tag + category with tax added 83 $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id" ) ); 84 $this->assertQueryTrue( 'is_category', 'is_tag', 'is_archive' ); 85 $this->assertNotEmpty( get_query_var( 'tax_query' ) ); 86 $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); 87 $this->assertNotEmpty( get_query_var( 'term_id' ) ); 88 $this->assertNotEmpty( get_query_var( 'cat' ) ); 89 $this->assertNotEmpty( get_query_var( 'tag_id' ) ); 90 $this->assertEquals( get_queried_object(), $this->cat ); 91 } 92 93 function test_tag_query_cat_query_tax_action_tax() { 94 // tag + category + tax with tax added 95 $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id&testtax=tax-slug2" ) ); 96 $this->assertQueryTrue( 'is_category', 'is_tag', 'is_tax', 'is_archive' ); 97 $this->assertNotEmpty( get_query_var( 'tax_query' ) ); 98 $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); 99 $this->assertNotEmpty( get_query_var( 'term_id' ) ); 100 $this->assertNotEmpty( get_query_var( 'cat' ) ); 101 $this->assertNotEmpty( get_query_var( 'tag_id' ) ); 102 $this->assertNotEmpty( get_query_var( 'testtax' ) ); 103 $this->assertEquals( get_queried_object(), $this->cat ); 104 } 105 106 function test_cat_action_tax() { 107 // category with tax added 108 $this->go_to( home_url( "/category/cat-slug/" ) ); 109 $this->assertQueryTrue( 'is_category', 'is_archive' ); 110 $this->assertNotEmpty( get_query_var( 'cat' ) ); 111 $this->assertNotEmpty( get_query_var( 'tax_query' ) ); 112 $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); 113 $this->assertNotEmpty( get_query_var( 'term_id' ) ); 114 $this->assertEquals( get_queried_object(), $this->cat ); 8 public function test_tax_query_single_query_single_term_field_slug() { 9 $t = $this->factory->term->create( array( 10 'taxonomy' => 'category', 11 'slug' => 'foo', 12 'name' => 'Foo', 13 ) ); 14 $p1 = $this->factory->post->create(); 15 $p2 = $this->factory->post->create(); 16 17 wp_set_post_terms( $p1, $t, 'category' ); 18 19 $q = new WP_Query( array( 20 'fields' => 'ids', 21 'update_post_meta_cache' => false, 22 'update_post_term_cache' => false, 23 'tax_query' => array( 24 array( 25 'taxonomy' => 'category', 26 'terms' => array( 'foo' ), 27 'field' => 'slug', 28 ), 29 ), 30 ) ); 31 32 $this->assertEquals( array( $p1 ), $q->posts ); 33 } 34 35 public function test_tax_query_single_query_single_term_field_name() { 36 $t = $this->factory->term->create( array( 37 'taxonomy' => 'category', 38 'slug' => 'foo', 39 'name' => 'Foo', 40 ) ); 41 $p1 = $this->factory->post->create(); 42 $p2 = $this->factory->post->create(); 43 44 wp_set_post_terms( $p1, $t, 'category' ); 45 46 $q = new WP_Query( array( 47 'fields' => 'ids', 48 'update_post_meta_cache' => false, 49 'update_post_term_cache' => false, 50 'tax_query' => array( 51 array( 52 'taxonomy' => 'category', 53 'terms' => array( 'Foo' ), 54 'field' => 'name', 55 ), 56 ), 57 ) ); 58 59 $this->assertEquals( array( $p1 ), $q->posts ); 60 } 61 62 public function test_tax_query_single_query_single_term_field_term_taxonomy_id() { 63 $t = $this->factory->term->create( array( 64 'taxonomy' => 'category', 65 'slug' => 'foo', 66 'name' => 'Foo', 67 ) ); 68 $p1 = $this->factory->post->create(); 69 $p2 = $this->factory->post->create(); 70 71 $tt_ids = wp_set_post_terms( $p1, $t, 'category' ); 72 73 $q = new WP_Query( array( 74 'fields' => 'ids', 75 'update_post_meta_cache' => false, 76 'update_post_term_cache' => false, 77 'tax_query' => array( 78 array( 79 'taxonomy' => 'category', 80 'terms' => $tt_ids, 81 'field' => 'term_taxonomy_id', 82 ), 83 ), 84 ) ); 85 86 $this->assertEquals( array( $p1 ), $q->posts ); 87 } 88 89 public function test_tax_query_single_query_single_term_field_term_id() { 90 $t = $this->factory->term->create( array( 91 'taxonomy' => 'category', 92 'slug' => 'foo', 93 'name' => 'Foo', 94 ) ); 95 $p1 = $this->factory->post->create(); 96 $p2 = $this->factory->post->create(); 97 98 wp_set_post_terms( $p1, $t, 'category' ); 99 100 $q = new WP_Query( array( 101 'fields' => 'ids', 102 'update_post_meta_cache' => false, 103 'update_post_term_cache' => false, 104 'tax_query' => array( 105 array( 106 'taxonomy' => 'category', 107 'terms' => array( $t ), 108 'field' => 'term_id', 109 ), 110 ), 111 ) ); 112 113 $this->assertEquals( array( $p1 ), $q->posts ); 114 } 115 116 public function test_tax_query_single_query_single_term_operator_in() { 117 $t = $this->factory->term->create( array( 118 'taxonomy' => 'category', 119 'slug' => 'foo', 120 'name' => 'Foo', 121 ) ); 122 $p1 = $this->factory->post->create(); 123 $p2 = $this->factory->post->create(); 124 125 wp_set_post_terms( $p1, $t, 'category' ); 126 127 $q = new WP_Query( array( 128 'fields' => 'ids', 129 'update_post_meta_cache' => false, 130 'update_post_term_cache' => false, 131 'tax_query' => array( 132 array( 133 'taxonomy' => 'category', 134 'terms' => array( 'foo' ), 135 'field' => 'slug', 136 'operator' => 'IN', 137 ), 138 ), 139 ) ); 140 141 $this->assertEquals( array( $p1 ), $q->posts ); 142 } 143 144 public function test_tax_query_single_query_single_term_operator_not_in() { 145 $t = $this->factory->term->create( array( 146 'taxonomy' => 'category', 147 'slug' => 'foo', 148 'name' => 'Foo', 149 ) ); 150 $p1 = $this->factory->post->create(); 151 $p2 = $this->factory->post->create(); 152 153 wp_set_post_terms( $p1, $t, 'category' ); 154 155 $q = new WP_Query( array( 156 'fields' => 'ids', 157 'update_post_meta_cache' => false, 158 'update_post_term_cache' => false, 159 'tax_query' => array( 160 array( 161 'taxonomy' => 'category', 162 'terms' => array( 'foo' ), 163 'field' => 'slug', 164 'operator' => 'NOT IN', 165 ), 166 ), 167 ) ); 168 169 $this->assertEquals( array( $p2 ), $q->posts ); 170 } 171 172 public function test_tax_query_single_query_single_term_operator_and() { 173 $t = $this->factory->term->create( array( 174 'taxonomy' => 'category', 175 'slug' => 'foo', 176 'name' => 'Foo', 177 ) ); 178 $p1 = $this->factory->post->create(); 179 $p2 = $this->factory->post->create(); 180 181 wp_set_post_terms( $p1, $t, 'category' ); 182 183 $q = new WP_Query( array( 184 'fields' => 'ids', 185 'update_post_meta_cache' => false, 186 'update_post_term_cache' => false, 187 'tax_query' => array( 188 array( 189 'taxonomy' => 'category', 190 'terms' => array( 'foo' ), 191 'field' => 'slug', 192 'operator' => 'AND', 193 ), 194 ), 195 ) ); 196 197 $this->assertEquals( array( $p1 ), $q->posts ); 198 } 199 200 public function test_tax_query_single_query_multiple_terms_operator_in() { 201 $t1 = $this->factory->term->create( array( 202 'taxonomy' => 'category', 203 'slug' => 'foo', 204 'name' => 'Foo', 205 ) ); 206 $t2 = $this->factory->term->create( array( 207 'taxonomy' => 'category', 208 'slug' => 'bar', 209 'name' => 'Bar', 210 ) ); 211 $p1 = $this->factory->post->create(); 212 $p2 = $this->factory->post->create(); 213 $p3 = $this->factory->post->create(); 214 215 wp_set_post_terms( $p1, $t1, 'category' ); 216 wp_set_post_terms( $p2, $t2, 'category' ); 217 218 $q = new WP_Query( array( 219 'fields' => 'ids', 220 'update_post_meta_cache' => false, 221 'update_post_term_cache' => false, 222 'tax_query' => array( 223 array( 224 'taxonomy' => 'category', 225 'terms' => array( 'foo', 'bar' ), 226 'field' => 'slug', 227 'operator' => 'IN', 228 ), 229 ), 230 ) ); 231 232 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); 233 } 234 235 public function test_tax_query_single_query_multiple_terms_operator_not_in() { 236 $t1 = $this->factory->term->create( array( 237 'taxonomy' => 'category', 238 'slug' => 'foo', 239 'name' => 'Foo', 240 ) ); 241 $t2 = $this->factory->term->create( array( 242 'taxonomy' => 'category', 243 'slug' => 'bar', 244 'name' => 'Bar', 245 ) ); 246 $p1 = $this->factory->post->create(); 247 $p2 = $this->factory->post->create(); 248 $p3 = $this->factory->post->create(); 249 250 wp_set_post_terms( $p1, $t1, 'category' ); 251 wp_set_post_terms( $p2, $t2, 'category' ); 252 253 $q = new WP_Query( array( 254 'fields' => 'ids', 255 'update_post_meta_cache' => false, 256 'update_post_term_cache' => false, 257 'tax_query' => array( 258 array( 259 'taxonomy' => 'category', 260 'terms' => array( 'foo', 'bar' ), 261 'field' => 'slug', 262 'operator' => 'NOT IN', 263 ), 264 ), 265 ) ); 266 267 $this->assertEquals( array( $p3 ), $q->posts ); 115 268 } 116 269 117 270 /** 118 * @ticket 26627271 * @ticket 18105 119 272 */ 120 function test_cat_uncat_action_tax() { 121 // category with tax added 122 add_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 ); 123 124 $this->go_to( home_url( "/category/uncategorized/" ) ); 125 $this->assertQueryTrue( 'is_category', 'is_archive' ); 126 $this->assertNotEmpty( get_query_var( 'cat' ) ); 127 $this->assertNotEmpty( get_query_var( 'tax_query' ) ); 128 $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); 129 $this->assertNotEmpty( get_query_var( 'term_id' ) ); 130 $this->assertEquals( get_queried_object(), $this->uncat ); 131 132 remove_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 ); 133 } 134 135 function _cat_uncat_action_tax( &$query ) { 136 $this->assertTrue( $query->is_category() ); 137 $this->assertTrue( $query->is_archive() ); 138 $this->assertNotEmpty( $query->get( 'category_name' ) ); 139 $this->assertNotEmpty( $query->get( 'tax_query' ) ); 140 $this->assertEquals( $query->get_queried_object(), $this->uncat ); 273 public function test_tax_query_single_query_multiple_queries_operator_not_in() { 274 $t1 = $this->factory->term->create( array( 275 'taxonomy' => 'category', 276 'slug' => 'foo', 277 'name' => 'Foo', 278 ) ); 279 $t2 = $this->factory->term->create( array( 280 'taxonomy' => 'category', 281 'slug' => 'bar', 282 'name' => 'Bar', 283 ) ); 284 $p1 = $this->factory->post->create(); 285 $p2 = $this->factory->post->create(); 286 $p3 = $this->factory->post->create(); 287 288 wp_set_post_terms( $p1, $t1, 'category' ); 289 wp_set_post_terms( $p2, $t2, 'category' ); 290 291 $q = new WP_Query( array( 292 'fields' => 'ids', 293 'update_post_meta_cache' => false, 294 'update_post_term_cache' => false, 295 'tax_query' => array( 296 'relation' => 'AND', 297 array( 298 'taxonomy' => 'category', 299 'terms' => array( 'foo' ), 300 'field' => 'slug', 301 'operator' => 'NOT IN', 302 ), 303 array( 304 'taxonomy' => 'category', 305 'terms' => array( 'bar' ), 306 'field' => 'slug', 307 'operator' => 'NOT IN', 308 ), 309 ), 310 ) ); 311 312 $this->assertEquals( array( $p3 ), $q->posts ); 313 } 314 315 public function test_tax_query_single_query_multiple_terms_operator_and() { 316 $t1 = $this->factory->term->create( array( 317 'taxonomy' => 'category', 318 'slug' => 'foo', 319 'name' => 'Foo', 320 ) ); 321 $t2 = $this->factory->term->create( array( 322 'taxonomy' => 'category', 323 'slug' => 'bar', 324 'name' => 'Bar', 325 ) ); 326 $p1 = $this->factory->post->create(); 327 $p2 = $this->factory->post->create(); 328 $p3 = $this->factory->post->create(); 329 330 wp_set_object_terms( $p1, $t1, 'category' ); 331 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); 332 333 $q = new WP_Query( array( 334 'fields' => 'ids', 335 'update_post_meta_cache' => false, 336 'update_post_term_cache' => false, 337 'tax_query' => array( 338 array( 339 'taxonomy' => 'category', 340 'terms' => array( 'foo', 'bar' ), 341 'field' => 'slug', 342 'operator' => 'AND', 343 ), 344 ), 345 ) ); 346 347 $this->assertEquals( array( $p2 ), $q->posts ); 141 348 } 142 349 143 350 /** 144 * @ticket 2 6728351 * @ticket 29181 145 352 */ 146 function test_tax_action_tax() { 147 // tax with tax added 148 $this->go_to( home_url( '/testtax/tax-slug2/' ) ); 149 $this->assertQueryTrue( 'is_tax', 'is_archive' ); 150 $this->assertNotEmpty( get_query_var( 'tax_query' ) ); 151 $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); 152 $this->assertNotEmpty( get_query_var( 'term_id' ) ); 153 $this->assertEquals( get_queried_object(), get_term( $this->tax_id, 'testtax' ) ); 154 } 155 156 function test_tax_query_tag_action_tax() { 157 // tax + tag with tax added 158 $this->go_to( home_url( "/testtax/tax-slug2/?tag_id=$this->tag_id" ) ); 159 $this->assertQueryTrue( 'is_tag', 'is_tax', 'is_archive' ); 160 $this->assertNotEmpty( get_query_var( 'tax_query' ) ); 161 $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); 162 $this->assertNotEmpty( get_query_var( 'term_id' ) ); 163 $this->assertNotEmpty( get_query_var( 'tag_id' ) ); 164 $this->assertEquals( get_queried_object(), $this->tag ); 165 } 166 167 function test_tax_query_cat_action_tax() { 168 // tax + cat with tax added 169 $this->go_to( home_url( "/testtax/tax-slug2/?cat=$this->cat_id" ) ); 170 $this->assertQueryTrue( 'is_category', 'is_tax', 'is_archive' ); 171 $this->assertNotEmpty( get_query_var( 'tax_query' ) ); 172 $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); 173 $this->assertNotEmpty( get_query_var( 'term_id' ) ); 174 $this->assertNotEmpty( get_query_var( 'cat' ) ); 175 $this->assertEquals( get_queried_object(), $this->cat ); 176 } 177 178 function pre_get_posts_tax_category_tax_query( &$query ) { 179 $query->set( 'tax_query', array( 180 array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $this->tax_id ) 181 ) ); 353 public function test_tax_query_operator_not_exists() { 354 register_taxonomy( 'wptests_tax1', 'post' ); 355 register_taxonomy( 'wptests_tax2', 'post' ); 356 357 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); 358 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); 359 360 $p1 = $this->factory->post->create(); 361 $p2 = $this->factory->post->create(); 362 $p3 = $this->factory->post->create(); 363 364 wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); 365 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); 366 367 $q = new WP_Query( array( 368 'fields' => 'ids', 369 'orderby' => 'ID', 370 'order' => 'ASC', 371 'tax_query' => array( 372 array( 373 'taxonomy' => 'wptests_tax2', 374 'operator' => 'NOT EXISTS', 375 ), 376 ), 377 ) ); 378 379 $this->assertEqualSets( array( $p1, $p3 ), $q->posts ); 182 380 } 183 381 184 382 /** 185 * @ group 30623383 * @ticket 29181 186 384 */ 187 public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_term_id_should_return_term_object() { 188 // Don't override the args provided below. 189 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); 190 191 $args = array( 385 public function test_tax_query_operator_exists() { 386 register_taxonomy( 'wptests_tax1', 'post' ); 387 register_taxonomy( 'wptests_tax2', 'post' ); 388 389 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); 390 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); 391 392 $p1 = $this->factory->post->create(); 393 $p2 = $this->factory->post->create(); 394 $p3 = $this->factory->post->create(); 395 396 wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); 397 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); 398 399 $q = new WP_Query( array( 400 'fields' => 'ids', 401 'orderby' => 'ID', 402 'order' => 'ASC', 403 'tax_query' => array( 404 array( 405 'taxonomy' => 'wptests_tax2', 406 'operator' => 'EXISTS', 407 ), 408 ), 409 ) ); 410 411 $this->assertEqualSets( array( $p2 ), $q->posts ); 412 } 413 414 /** 415 * @ticket 29181 416 */ 417 public function test_tax_query_operator_exists_should_ignore_terms() { 418 register_taxonomy( 'wptests_tax1', 'post' ); 419 register_taxonomy( 'wptests_tax2', 'post' ); 420 421 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); 422 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); 423 424 $p1 = $this->factory->post->create(); 425 $p2 = $this->factory->post->create(); 426 $p3 = $this->factory->post->create(); 427 428 wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); 429 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); 430 431 $q = new WP_Query( array( 432 'fields' => 'ids', 433 'orderby' => 'ID', 434 'order' => 'ASC', 435 'tax_query' => array( 436 array( 437 'taxonomy' => 'wptests_tax2', 438 'operator' => 'EXISTS', 439 'terms' => array( 'foo', 'bar' ), 440 ), 441 ), 442 ) ); 443 444 $this->assertEqualSets( array( $p2 ), $q->posts ); 445 } 446 447 /** 448 * @ticket 29181 449 */ 450 public function test_tax_query_operator_exists_with_no_taxonomy() { 451 register_taxonomy( 'wptests_tax1', 'post' ); 452 register_taxonomy( 'wptests_tax2', 'post' ); 453 454 $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); 455 $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); 456 457 $p1 = $this->factory->post->create(); 458 $p2 = $this->factory->post->create(); 459 $p3 = $this->factory->post->create(); 460 461 wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); 462 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); 463 464 $q = new WP_Query( array( 465 'fields' => 'ids', 466 'orderby' => 'ID', 467 'order' => 'ASC', 468 'tax_query' => array( 469 array( 470 'operator' => 'EXISTS', 471 ), 472 ), 473 ) ); 474 475 $this->assertEmpty( $q->posts ); 476 } 477 478 public function test_tax_query_multiple_queries_relation_and() { 479 $t1 = $this->factory->term->create( array( 480 'taxonomy' => 'category', 481 'slug' => 'foo', 482 'name' => 'Foo', 483 ) ); 484 $t2 = $this->factory->term->create( array( 485 'taxonomy' => 'category', 486 'slug' => 'bar', 487 'name' => 'Bar', 488 ) ); 489 $p1 = $this->factory->post->create(); 490 $p2 = $this->factory->post->create(); 491 $p3 = $this->factory->post->create(); 492 493 wp_set_object_terms( $p1, $t1, 'category' ); 494 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); 495 496 $q = new WP_Query( array( 497 'fields' => 'ids', 498 'update_post_meta_cache' => false, 499 'update_post_term_cache' => false, 192 500 'tax_query' => array( 193 501 'relation' => 'AND', 194 502 array( 195 'taxonomy' => 'testtax', 503 'taxonomy' => 'category', 504 'terms' => array( 'foo' ), 505 'field' => 'slug', 506 ), 507 array( 508 'taxonomy' => 'category', 509 'terms' => array( 'bar' ), 510 'field' => 'slug', 511 ), 512 ), 513 ) ); 514 515 $this->assertEquals( array( $p2 ), $q->posts ); 516 } 517 518 public function test_tax_query_multiple_queries_relation_or() { 519 $t1 = $this->factory->term->create( array( 520 'taxonomy' => 'category', 521 'slug' => 'foo', 522 'name' => 'Foo', 523 ) ); 524 $t2 = $this->factory->term->create( array( 525 'taxonomy' => 'category', 526 'slug' => 'bar', 527 'name' => 'Bar', 528 ) ); 529 $p1 = $this->factory->post->create(); 530 $p2 = $this->factory->post->create(); 531 $p3 = $this->factory->post->create(); 532 533 wp_set_object_terms( $p1, $t1, 'category' ); 534 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); 535 536 $q = new WP_Query( array( 537 'fields' => 'ids', 538 'update_post_meta_cache' => false, 539 'update_post_term_cache' => false, 540 'tax_query' => array( 541 'relation' => 'OR', 542 array( 543 'taxonomy' => 'category', 544 'terms' => array( 'foo' ), 545 'field' => 'slug', 546 ), 547 array( 548 'taxonomy' => 'category', 549 'terms' => array( 'bar' ), 550 'field' => 'slug', 551 ), 552 ), 553 ) ); 554 555 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); 556 } 557 558 public function test_tax_query_multiple_queries_different_taxonomies() { 559 $t1 = $this->factory->term->create( array( 560 'taxonomy' => 'post_tag', 561 'slug' => 'foo', 562 'name' => 'Foo', 563 ) ); 564 $t2 = $this->factory->term->create( array( 565 'taxonomy' => 'category', 566 'slug' => 'bar', 567 'name' => 'Bar', 568 ) ); 569 $p1 = $this->factory->post->create(); 570 $p2 = $this->factory->post->create(); 571 $p3 = $this->factory->post->create(); 572 573 wp_set_object_terms( $p1, $t1, 'post_tag' ); 574 wp_set_object_terms( $p2, $t2, 'category' ); 575 576 $q = new WP_Query( array( 577 'fields' => 'ids', 578 'update_post_meta_cache' => false, 579 'update_post_term_cache' => false, 580 'tax_query' => array( 581 'relation' => 'OR', 582 array( 583 'taxonomy' => 'post_tag', 584 'terms' => array( 'foo' ), 585 'field' => 'slug', 586 ), 587 array( 588 'taxonomy' => 'category', 589 'terms' => array( 'bar' ), 590 'field' => 'slug', 591 ), 592 ), 593 ) ); 594 595 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); 596 } 597 598 /** 599 * @ticket 29738 600 */ 601 public function test_tax_query_two_nested_queries() { 602 register_taxonomy( 'foo', 'post' ); 603 register_taxonomy( 'bar', 'post' ); 604 605 $foo_term_1 = $this->factory->term->create( array( 606 'taxonomy' => 'foo', 607 ) ); 608 $foo_term_2 = $this->factory->term->create( array( 609 'taxonomy' => 'foo', 610 ) ); 611 $bar_term_1 = $this->factory->term->create( array( 612 'taxonomy' => 'bar', 613 ) ); 614 $bar_term_2 = $this->factory->term->create( array( 615 'taxonomy' => 'bar', 616 ) ); 617 618 $p1 = $this->factory->post->create(); 619 $p2 = $this->factory->post->create(); 620 $p3 = $this->factory->post->create(); 621 622 wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); 623 wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); 624 wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); 625 wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); 626 wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); 627 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); 628 629 $q = new WP_Query( array( 630 'fields' => 'ids', 631 'update_post_meta_cache' => false, 632 'update_post_term_cache' => false, 633 'tax_query' => array( 634 'relation' => 'OR', 635 array( 636 'relation' => 'AND', 637 array( 638 'taxonomy' => 'foo', 639 'terms' => array( $foo_term_1 ), 640 'field' => 'term_id', 641 ), 642 array( 643 'taxonomy' => 'bar', 644 'terms' => array( $bar_term_1 ), 645 'field' => 'term_id', 646 ), 647 ), 648 array( 649 'relation' => 'AND', 650 array( 651 'taxonomy' => 'foo', 652 'terms' => array( $foo_term_2 ), 653 'field' => 'term_id', 654 ), 655 array( 656 'taxonomy' => 'bar', 657 'terms' => array( $bar_term_2 ), 658 'field' => 'term_id', 659 ), 660 ), 661 ), 662 ) ); 663 664 _unregister_taxonomy( 'foo' ); 665 _unregister_taxonomy( 'bar' ); 666 667 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); 668 } 669 670 /** 671 * @ticket 29738 672 */ 673 public function test_tax_query_one_nested_query_one_first_order_query() { 674 register_taxonomy( 'foo', 'post' ); 675 register_taxonomy( 'bar', 'post' ); 676 677 $foo_term_1 = $this->factory->term->create( array( 678 'taxonomy' => 'foo', 679 ) ); 680 $foo_term_2 = $this->factory->term->create( array( 681 'taxonomy' => 'foo', 682 ) ); 683 $bar_term_1 = $this->factory->term->create( array( 684 'taxonomy' => 'bar', 685 ) ); 686 $bar_term_2 = $this->factory->term->create( array( 687 'taxonomy' => 'bar', 688 ) ); 689 690 $p1 = $this->factory->post->create(); 691 $p2 = $this->factory->post->create(); 692 $p3 = $this->factory->post->create(); 693 694 wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); 695 wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); 696 wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); 697 wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); 698 wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); 699 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); 700 701 $q = new WP_Query( array( 702 'fields' => 'ids', 703 'update_post_meta_cache' => false, 704 'update_post_term_cache' => false, 705 'tax_query' => array( 706 'relation' => 'OR', 707 array( 708 'taxonomy' => 'foo', 709 'terms' => array( $foo_term_2 ), 196 710 'field' => 'term_id', 197 'terms' => array( 198 $this->tax_id, 711 ), 712 array( 713 'relation' => 'AND', 714 array( 715 'taxonomy' => 'foo', 716 'terms' => array( $foo_term_1 ), 717 'field' => 'term_id', 199 718 ), 719 array( 720 'taxonomy' => 'bar', 721 'terms' => array( $bar_term_1 ), 722 'field' => 'term_id', 723 ), 724 ), 725 ), 726 ) ); 727 728 _unregister_taxonomy( 'foo' ); 729 _unregister_taxonomy( 'bar' ); 730 731 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); 732 } 733 734 /** 735 * @ticket 29738 736 */ 737 public function test_tax_query_one_double_nested_query_one_first_order_query() { 738 register_taxonomy( 'foo', 'post' ); 739 register_taxonomy( 'bar', 'post' ); 740 741 $foo_term_1 = $this->factory->term->create( array( 742 'taxonomy' => 'foo', 743 ) ); 744 $foo_term_2 = $this->factory->term->create( array( 745 'taxonomy' => 'foo', 746 ) ); 747 $bar_term_1 = $this->factory->term->create( array( 748 'taxonomy' => 'bar', 749 ) ); 750 $bar_term_2 = $this->factory->term->create( array( 751 'taxonomy' => 'bar', 752 ) ); 753 754 $p1 = $this->factory->post->create(); 755 $p2 = $this->factory->post->create(); 756 $p3 = $this->factory->post->create(); 757 $p4 = $this->factory->post->create(); 758 759 wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); 760 wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); 761 wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); 762 wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); 763 wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); 764 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); 765 766 $q = new WP_Query( array( 767 'fields' => 'ids', 768 'update_post_meta_cache' => false, 769 'update_post_term_cache' => false, 770 'tax_query' => array( 771 'relation' => 'OR', 772 array( 773 'taxonomy' => 'foo', 774 'terms' => array( $foo_term_2 ), 775 'field' => 'term_id', 776 ), 777 array( 778 'relation' => 'AND', 779 array( 780 'taxonomy' => 'foo', 781 'terms' => array( $foo_term_1 ), 782 'field' => 'term_id', 783 ), 784 array( 785 'relation' => 'OR', 786 array( 787 'taxonomy' => 'bar', 788 'terms' => array( $bar_term_1 ), 789 'field' => 'term_id', 790 ), 791 array( 792 'taxonomy' => 'bar', 793 'terms' => array( $bar_term_2 ), 794 'field' => 'term_id', 795 ), 796 ), 797 ), 798 ), 799 ) ); 800 801 _unregister_taxonomy( 'foo' ); 802 _unregister_taxonomy( 'bar' ); 803 804 $this->assertEqualSets( array( $p1, $p2, $p3 ), $q->posts ); 805 } 806 807 /** 808 * @ticket 20604 809 */ 810 public function test_tax_query_relation_or_both_clauses_empty_terms() { 811 // An empty tax query should return an empty array, not all posts. 812 813 $this->factory->post->create_many( 10 ); 814 815 $query = new WP_Query( array( 816 'fields' => 'ids', 817 'update_post_term_cache' => false, 818 'update_post_meta_cache' => false, 819 'tax_query' => array( 820 'relation' => 'OR', 821 array( 822 'taxonomy' => 'post_tag', 823 'field' => 'id', 824 'terms' => false, 825 'operator' => 'IN' 826 ), 827 array( 828 'taxonomy' => 'category', 829 'field' => 'id', 830 'terms' => false, 831 'operator' => 'IN' 200 832 ), 201 833 ) 202 ); 203 204 $q = new WP_Query( $args ); 205 $object = $q->get_queried_object(); 206 207 $expected = get_term( $this->tax_id, 'testtax' ); 208 209 $this->assertEquals( $expected, $object ); 834 ) ); 835 836 $posts = $query->get_posts(); 837 $this->assertEquals( 0 , count( $posts ) ); 210 838 } 211 839 212 840 /** 213 * @ group 30623841 * @ticket 20604 214 842 */ 215 public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_slug_should_return_term_object() { 216 // Don't override the args provided below. 217 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); 218 219 $args = array( 843 public function test_tax_query_relation_or_one_clause_empty_terms() { 844 // An empty tax query should return an empty array, not all posts. 845 846 $this->factory->post->create_many( 10 ); 847 848 $query = new WP_Query( array( 849 'fields' => 'ids', 850 'update_post_term_cache' => false, 851 'update_post_meta_cache' => false, 852 'tax_query' => array( 853 'relation' => 'OR', 854 array( 855 'taxonomy' => 'post_tag', 856 'field' => 'id', 857 'terms' => array( 'foo' ), 858 'operator' => 'IN' 859 ), 860 array( 861 'taxonomy' => 'category', 862 'field' => 'id', 863 'terms' => false, 864 'operator' => 'IN' 865 ), 866 ) 867 ) ); 868 869 $posts = $query->get_posts(); 870 $this->assertEquals( 0 , count( $posts ) ); 871 } 872 873 public function test_tax_query_include_children() { 874 $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) ); 875 $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) ); 876 $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'East Syndney', 'parent' => $cat_b ) ); 877 $cat_d = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'West Syndney', 'parent' => $cat_b ) ); 878 879 $post_a = $this->factory->post->create( array( 'post_category' => array( $cat_a ) ) ); 880 $post_b = $this->factory->post->create( array( 'post_category' => array( $cat_b ) ) ); 881 $post_c = $this->factory->post->create( array( 'post_category' => array( $cat_c ) ) ); 882 $post_d = $this->factory->post->create( array( 'post_category' => array( $cat_d ) ) ); 883 884 $posts = get_posts( array( 885 'fields' => 'ids', 886 'update_post_meta_cache' => false, 887 'update_post_term_cache' => false, 888 'tax_query' => array( 889 array( 890 'taxonomy' => 'category', 891 'field' => 'id', 892 'terms' => array( $cat_a ), 893 ) 894 ) 895 ) ); 896 897 $this->assertEquals( 4 , count( $posts ) ); 898 899 $posts = get_posts( array( 900 'fields' => 'ids', 901 'update_post_meta_cache' => false, 902 'update_post_term_cache' => false, 903 'tax_query' => array( 904 array( 905 'taxonomy' => 'category', 906 'field' => 'id', 907 'terms' => array( $cat_a ), 908 'include_children' => false 909 ) 910 ) 911 ) ); 912 913 $this->assertEquals( 1 , count( $posts ) ); 914 915 $posts = get_posts( array( 916 'fields' => 'ids', 917 'update_post_meta_cache' => false, 918 'update_post_term_cache' => false, 919 'tax_query' => array( 920 array( 921 'taxonomy' => 'category', 922 'field' => 'id', 923 'terms' => array( $cat_b ), 924 ) 925 ) 926 ) ); 927 928 $this->assertEquals( 3 , count( $posts ) ); 929 930 $posts = get_posts( array( 931 'fields' => 'ids', 932 'update_post_meta_cache' => false, 933 'update_post_term_cache' => false, 934 'tax_query' => array( 935 array( 936 'taxonomy' => 'category', 937 'field' => 'id', 938 'terms' => array( $cat_b ), 939 'include_children' => false 940 ) 941 ) 942 ) ); 943 944 $this->assertEquals( 1 , count( $posts ) ); 945 946 $posts = get_posts( array( 947 'fields' => 'ids', 948 'update_post_meta_cache' => false, 949 'update_post_term_cache' => false, 950 'tax_query' => array( 951 array( 952 'taxonomy' => 'category', 953 'field' => 'id', 954 'terms' => array( $cat_c ), 955 ) 956 ) 957 ) ); 958 959 $this->assertEquals( 1 , count( $posts ) ); 960 961 $posts = get_posts( array( 962 'fields' => 'ids', 963 'update_post_meta_cache' => false, 964 'update_post_term_cache' => false, 965 'tax_query' => array( 966 array( 967 'taxonomy' => 'category', 968 'field' => 'id', 969 'terms' => array( $cat_c ), 970 'include_children' => false 971 ) 972 ) 973 ) ); 974 975 $this->assertEquals( 1 , count( $posts ) ); 976 } 977 978 public function test_tax_query_taxonomy_with_attachments() { 979 $q = new WP_Query(); 980 981 register_taxonomy_for_object_type( 'post_tag', 'attachment:image' ); 982 $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) ); 983 $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array( 984 'post_mime_type' => 'image/jpeg', 985 'post_type' => 'attachment' 986 ) ); 987 wp_set_object_terms( $image_id, $tag_id, 'post_tag' ); 988 989 $posts = $q->query( array( 990 'fields' => 'ids', 991 'update_post_meta_cache' => false, 992 'update_post_term_cache' => false, 993 'post_type' => 'attachment', 994 'post_status' => 'inherit', 995 'tax_query' => array( 996 array( 997 'taxonomy' => 'post_tag', 998 'field' => 'term_id', 999 'terms' => array( $tag_id ) 1000 ) 1001 ) 1002 ) ); 1003 1004 $this->assertEquals( array( $image_id ), $posts ); 1005 } 1006 1007 public function test_tax_query_no_taxonomy() { 1008 $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) ); 1009 $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) ); 1010 1011 $response1 = new WP_Query( array( 1012 'tax_query' => array( 1013 array( 'terms' => array( $cat_id ) ) 1014 ) 1015 ) ); 1016 $this->assertEmpty( $response1->posts ); 1017 1018 $response2 = new WP_Query( array( 1019 'fields' => 'ids', 1020 'update_post_meta_cache' => false, 1021 'update_post_term_cache' => false, 1022 'tax_query' => array( 1023 array( 1024 'taxonomy' => 'category', 1025 'terms' => array( $cat_id ) 1026 ) 1027 ) 1028 ) ); 1029 $this->assertNotEmpty( $response2->posts ); 1030 1031 $term = get_category( $cat_id ); 1032 $response3 = new WP_Query( array( 1033 'fields' => 'ids', 1034 'update_post_meta_cache' => false, 1035 'update_post_term_cache' => false, 1036 'tax_query' => array( 1037 array( 1038 'field' => 'term_taxonomy_id', 1039 'terms' => array( $term->term_taxonomy_id ) 1040 ) 1041 ) 1042 ) ); 1043 $this->assertNotEmpty( $response3->posts ); 1044 } 1045 1046 public function test_term_taxonomy_id_field_no_taxonomy() { 1047 $q = new WP_Query(); 1048 1049 $posts = $this->factory->post->create_many( 5 ); 1050 1051 $cats = $tags = array(); 1052 1053 // need term_taxonomy_ids in addition to term_ids, so no factory 1054 for ( $i = 0; $i < 5; $i++ ) { 1055 $cats[$i] = wp_insert_term( 'category-' . $i , 'category' ); 1056 $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' ); 1057 1058 // post 0 gets all terms 1059 wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true ); 1060 wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true ); 1061 } 1062 1063 wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); 1064 wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' ); 1065 1066 wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' ); 1067 wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); 1068 1069 wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); 1070 wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); 1071 1072 $results1 = $q->query( array( 1073 'fields' => 'ids', 1074 'update_post_meta_cache' => false, 1075 'update_post_term_cache' => false, 1076 'orderby' => 'ID', 1077 'order' => 'ASC', 1078 'tax_query' => array( 1079 'relation' => 'OR', 1080 array( 1081 'field' => 'term_taxonomy_id', 1082 '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'] ), 1083 'operator' => 'AND', 1084 'include_children' => false, 1085 ), 1086 array( 1087 'field' => 'term_taxonomy_id', 1088 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), 1089 'operator' => 'AND', 1090 'include_children' => false, 1091 ) 1092 ) 1093 ) ); 1094 1095 $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' ); 1096 1097 $results2 = $q->query( array( 1098 'fields' => 'ids', 1099 'update_post_meta_cache' => false, 1100 'update_post_term_cache' => false, 1101 'orderby' => 'ID', 1102 'order' => 'ASC', 220 1103 'tax_query' => array( 221 1104 'relation' => 'AND', 222 1105 array( 223 'taxonomy' => 'testtax', 224 'field' => 'slug', 225 'terms' => array( 226 'tax-slug', 227 ), 228 ), 1106 'field' => 'term_taxonomy_id', 1107 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ), 1108 'operator' => 'IN', 1109 'include_children' => false, 1110 ), 1111 array( 1112 'field' => 'term_taxonomy_id', 1113 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), 1114 'operator' => 'IN', 1115 'include_children' => false, 1116 ) 229 1117 ) 230 ); 231 232 $q = new WP_Query( $args ); 233 $object = $q->get_queried_object(); 234 235 $expected = get_term( $this->tax_id, 'testtax' ); 236 237 // Only compare term_id because object_id may or may not be part of either value. 238 $this->assertEquals( $expected->term_id, $object->term_id ); 1118 ) ); 1119 1120 $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' ); 239 1121 } 240 1122 241 1123 /** 242 * @ group 306231124 * @ticket 29738 243 1125 */ 244 public function test_get_queried_object_with_custom_taxonomy_tax_query_with_multiple_clauses_should_return_term_object_corresponding_to_the_first_queried_tax() { 245 // Don't override the args provided below. 246 remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); 247 248 register_taxonomy( 'testtax2', 'post' ); 249 $testtax2_term_id = $this->factory->term->create( array( 250 'taxonomy' => 'testtax2', 251 'slug' => 'testtax2-slug', 252 ) ); 253 254 $args = array( 255 'tax_query' => array( 256 'relation' => 'AND', 257 array( 258 'taxonomy' => 'testtax', 259 'field' => 'slug', 260 'terms' => array( 261 'tax-slug', 262 ), 263 ), 264 array( 265 'taxonomy' => 'testtax2', 266 'field' => 'slug', 267 'terms' => array( 268 'testtax2-slug', 269 ), 270 ), 271 ) 272 ); 273 274 $q = new WP_Query( $args ); 275 $object = $q->get_queried_object(); 276 277 $expected = get_term( $this->tax_id, 'testtax' ); 278 279 // Only compare term_id because object_id may or may not be part of either value. 280 $this->assertEquals( $expected->term_id, $object->term_id ); 1126 public function test_populate_taxonomy_query_var_from_tax_query() { 1127 register_taxonomy( 'foo', 'post' ); 1128 $t = $this->factory->term->create( array( 1129 'taxonomy' => 'foo', 1130 ) ); 1131 $c = $this->factory->term->create( array( 1132 'taxonomy' => 'category', 1133 ) ); 1134 1135 $q = new WP_Query( array( 1136 'tax_query' => array( 1137 // Empty terms mean that this one should be skipped 1138 array( 1139 'taxonomy' => 'bar', 1140 'terms' => array(), 1141 ), 1142 1143 // Category and post tags should be skipped 1144 array( 1145 'taxonomy' => 'category', 1146 'terms' => array( $c ), 1147 ), 1148 1149 array( 1150 'taxonomy' => 'foo', 1151 'terms' => array( $t ), 1152 ), 1153 ), 1154 ) ); 1155 1156 $this->assertSame( 'foo', $q->get( 'taxonomy' ) ); 1157 1158 _unregister_taxonomy( 'foo' ); 1159 } 1160 1161 public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() { 1162 register_taxonomy( 'foo', 'post' ); 1163 register_taxonomy( 'foo1', 'post' ); 1164 $t = $this->factory->term->create( array( 1165 'taxonomy' => 'foo', 1166 ) ); 1167 1168 $q = new WP_Query( array( 1169 'taxonomy' => 'bar', 1170 'tax_query' => array( 1171 array( 1172 'taxonomy' => 'foo', 1173 'terms' => array( $t ), 1174 ), 1175 ), 1176 ) ); 1177 1178 $this->assertSame( 'bar', $q->get( 'taxonomy' ) ); 1179 1180 _unregister_taxonomy( 'foo' ); 1181 _unregister_taxonomy( 'foo1' ); 1182 } 1183 1184 public function test_populate_term_query_var_from_tax_query() { 1185 register_taxonomy( 'foo', 'post' ); 1186 $t = $this->factory->term->create( array( 1187 'taxonomy' => 'foo', 1188 'slug' => 'bar', 1189 ) ); 1190 1191 $q = new WP_Query( array( 1192 'tax_query' => array( 1193 array( 1194 'taxonomy' => 'foo', 1195 'terms' => array( 'bar' ), 1196 'field' => 'slug', 1197 ), 1198 ), 1199 ) ); 1200 1201 $this->assertSame( 'bar', $q->get( 'term' ) ); 1202 1203 _unregister_taxonomy( 'foo' ); 1204 } 1205 1206 public function test_populate_term_id_query_var_from_tax_query() { 1207 register_taxonomy( 'foo', 'post' ); 1208 $t = $this->factory->term->create( array( 1209 'taxonomy' => 'foo', 1210 'slug' => 'bar', 1211 ) ); 1212 1213 $q = new WP_Query( array( 1214 'tax_query' => array( 1215 array( 1216 'taxonomy' => 'foo', 1217 'terms' => array( $t ), 1218 'field' => 'term_id', 1219 ), 1220 ), 1221 ) ); 1222 1223 $this->assertEquals( $t, $q->get( 'term_id' ) ); 1224 1225 _unregister_taxonomy( 'foo' ); 1226 } 1227 1228 /** 1229 * @ticket 29738 1230 */ 1231 public function test_populate_cat_category_name_query_var_from_tax_query() { 1232 register_taxonomy( 'foo', 'post' ); 1233 $t = $this->factory->term->create( array( 1234 'taxonomy' => 'foo', 1235 ) ); 1236 $c = $this->factory->term->create( array( 1237 'taxonomy' => 'category', 1238 'slug' => 'bar', 1239 ) ); 1240 1241 $q = new WP_Query( array( 1242 'tax_query' => array( 1243 // Non-category should be skipped 1244 array( 1245 'taxonomy' => 'foo', 1246 'terms' => array( $t ), 1247 ), 1248 1249 // Empty terms mean that this one should be skipped 1250 array( 1251 'taxonomy' => 'category', 1252 'terms' => array(), 1253 ), 1254 1255 // Category and post tags should be skipped 1256 array( 1257 'taxonomy' => 'category', 1258 'terms' => array( $c ), 1259 ), 1260 ), 1261 ) ); 1262 1263 $this->assertEquals( $c, $q->get( 'cat' ) ); 1264 $this->assertEquals( 'bar', $q->get( 'category_name' ) ); 1265 1266 _unregister_taxonomy( 'foo' ); 1267 } 1268 1269 /** 1270 * @ticket 29738 1271 */ 1272 public function test_populate_tag_id_query_var_from_tax_query() { 1273 register_taxonomy( 'foo', 'post' ); 1274 $t = $this->factory->term->create( array( 1275 'taxonomy' => 'foo', 1276 ) ); 1277 $tag = $this->factory->term->create( array( 1278 'taxonomy' => 'post_tag', 1279 'slug' => 'bar', 1280 ) ); 1281 1282 $q = new WP_Query( array( 1283 'tax_query' => array( 1284 // Non-tag should be skipped 1285 array( 1286 'taxonomy' => 'foo', 1287 'terms' => array( $t ), 1288 ), 1289 1290 // Empty terms mean that this one should be skipped 1291 array( 1292 'taxonomy' => 'post_tag', 1293 'terms' => array(), 1294 ), 1295 1296 // Category and post tags should be skipped 1297 array( 1298 'taxonomy' => 'post_tag', 1299 'terms' => array( $tag ), 1300 ), 1301 ), 1302 ) ); 1303 1304 $this->assertEquals( $tag, $q->get( 'tag_id' ) ); 1305 1306 _unregister_taxonomy( 'foo' ); 281 1307 } 282 1308 }
Note: See TracChangeset
for help on using the changeset viewer.