Changeset 42343 for trunk/tests/phpunit/tests/query/taxQuery.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/query/taxQuery.php
r39662 r42343 7 7 class Tests_Query_TaxQuery extends WP_UnitTestCase { 8 8 public function test_tax_query_single_query_single_term_field_slug() { 9 $t = self::factory()->term->create( array( 10 'taxonomy' => 'category', 11 'slug' => 'foo', 12 'name' => 'Foo', 13 ) ); 9 $t = self::factory()->term->create( 10 array( 11 'taxonomy' => 'category', 12 'slug' => 'foo', 13 'name' => 'Foo', 14 ) 15 ); 14 16 $p1 = self::factory()->post->create(); 15 17 $p2 = self::factory()->post->create(); … … 17 19 wp_set_post_terms( $p1, $t, 'category' ); 18 20 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 ) ); 21 $q = new WP_Query( 22 array( 23 'fields' => 'ids', 24 'update_post_meta_cache' => false, 25 'update_post_term_cache' => false, 26 'tax_query' => array( 27 array( 28 'taxonomy' => 'category', 29 'terms' => array( 'foo' ), 30 'field' => 'slug', 31 ), 32 ), 33 ) 34 ); 31 35 32 36 $this->assertEquals( array( $p1 ), $q->posts ); … … 34 38 35 39 public function test_tax_query_single_query_single_term_field_name() { 36 $t = self::factory()->term->create( array( 37 'taxonomy' => 'category', 38 'slug' => 'foo', 39 'name' => 'Foo', 40 ) ); 40 $t = self::factory()->term->create( 41 array( 42 'taxonomy' => 'category', 43 'slug' => 'foo', 44 'name' => 'Foo', 45 ) 46 ); 41 47 $p1 = self::factory()->post->create(); 42 48 $p2 = self::factory()->post->create(); … … 44 50 wp_set_post_terms( $p1, $t, 'category' ); 45 51 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 ) ); 52 $q = new WP_Query( 53 array( 54 'fields' => 'ids', 55 'update_post_meta_cache' => false, 56 'update_post_term_cache' => false, 57 'tax_query' => array( 58 array( 59 'taxonomy' => 'category', 60 'terms' => array( 'Foo' ), 61 'field' => 'name', 62 ), 63 ), 64 ) 65 ); 58 66 59 67 $this->assertEquals( array( $p1 ), $q->posts ); … … 66 74 register_taxonomy( 'wptests_tax', 'post' ); 67 75 68 $t = self::factory()->term->create( array( 69 'taxonomy' => 'wptests_tax', 70 'slug' => 'foo', 71 'name' => 'Foo Bar', 72 ) ); 76 $t = self::factory()->term->create( 77 array( 78 'taxonomy' => 'wptests_tax', 79 'slug' => 'foo', 80 'name' => 'Foo Bar', 81 ) 82 ); 73 83 $p1 = self::factory()->post->create(); 74 84 $p2 = self::factory()->post->create(); … … 76 86 wp_set_object_terms( $p1, $t, 'wptests_tax' ); 77 87 78 $q = new WP_Query( array( 79 'fields' => 'ids', 80 'tax_query' => array( 81 array( 82 'taxonomy' => 'wptests_tax', 83 'terms' => array( 'Foo Bar' ), 84 'field' => 'name', 85 ), 86 ), 87 ) ); 88 $q = new WP_Query( 89 array( 90 'fields' => 'ids', 91 'tax_query' => array( 92 array( 93 'taxonomy' => 'wptests_tax', 94 'terms' => array( 'Foo Bar' ), 95 'field' => 'name', 96 ), 97 ), 98 ) 99 ); 88 100 89 101 $this->assertEquals( array( $p1 ), $q->posts ); … … 91 103 92 104 public function test_tax_query_single_query_single_term_field_term_taxonomy_id() { 93 $t = self::factory()->term->create( array( 94 'taxonomy' => 'category', 95 'slug' => 'foo', 96 'name' => 'Foo', 97 ) ); 105 $t = self::factory()->term->create( 106 array( 107 'taxonomy' => 'category', 108 'slug' => 'foo', 109 'name' => 'Foo', 110 ) 111 ); 98 112 $p1 = self::factory()->post->create(); 99 113 $p2 = self::factory()->post->create(); … … 101 115 $tt_ids = wp_set_post_terms( $p1, $t, 'category' ); 102 116 103 $q = new WP_Query( array( 104 'fields' => 'ids', 105 'update_post_meta_cache' => false, 106 'update_post_term_cache' => false, 107 'tax_query' => array( 108 array( 109 'taxonomy' => 'category', 110 'terms' => $tt_ids, 111 'field' => 'term_taxonomy_id', 112 ), 113 ), 114 ) ); 117 $q = new WP_Query( 118 array( 119 'fields' => 'ids', 120 'update_post_meta_cache' => false, 121 'update_post_term_cache' => false, 122 'tax_query' => array( 123 array( 124 'taxonomy' => 'category', 125 'terms' => $tt_ids, 126 'field' => 'term_taxonomy_id', 127 ), 128 ), 129 ) 130 ); 115 131 116 132 $this->assertEquals( array( $p1 ), $q->posts ); … … 118 134 119 135 public function test_tax_query_single_query_single_term_field_term_id() { 120 $t = self::factory()->term->create( array( 121 'taxonomy' => 'category', 122 'slug' => 'foo', 123 'name' => 'Foo', 124 ) ); 136 $t = self::factory()->term->create( 137 array( 138 'taxonomy' => 'category', 139 'slug' => 'foo', 140 'name' => 'Foo', 141 ) 142 ); 125 143 $p1 = self::factory()->post->create(); 126 144 $p2 = self::factory()->post->create(); … … 128 146 wp_set_post_terms( $p1, $t, 'category' ); 129 147 130 $q = new WP_Query( array( 131 'fields' => 'ids', 132 'update_post_meta_cache' => false, 133 'update_post_term_cache' => false, 134 'tax_query' => array( 135 array( 136 'taxonomy' => 'category', 137 'terms' => array( $t ), 138 'field' => 'term_id', 139 ), 140 ), 141 ) ); 148 $q = new WP_Query( 149 array( 150 'fields' => 'ids', 151 'update_post_meta_cache' => false, 152 'update_post_term_cache' => false, 153 'tax_query' => array( 154 array( 155 'taxonomy' => 'category', 156 'terms' => array( $t ), 157 'field' => 'term_id', 158 ), 159 ), 160 ) 161 ); 142 162 143 163 $this->assertEquals( array( $p1 ), $q->posts ); … … 145 165 146 166 public function test_tax_query_single_query_single_term_operator_in() { 147 $t = self::factory()->term->create( array( 148 'taxonomy' => 'category', 149 'slug' => 'foo', 150 'name' => 'Foo', 151 ) ); 167 $t = self::factory()->term->create( 168 array( 169 'taxonomy' => 'category', 170 'slug' => 'foo', 171 'name' => 'Foo', 172 ) 173 ); 152 174 $p1 = self::factory()->post->create(); 153 175 $p2 = self::factory()->post->create(); … … 155 177 wp_set_post_terms( $p1, $t, 'category' ); 156 178 157 $q = new WP_Query( array( 158 'fields' => 'ids', 159 'update_post_meta_cache' => false, 160 'update_post_term_cache' => false, 161 'tax_query' => array( 162 array( 163 'taxonomy' => 'category', 164 'terms' => array( 'foo' ), 165 'field' => 'slug', 166 'operator' => 'IN', 167 ), 168 ), 169 ) ); 179 $q = new WP_Query( 180 array( 181 'fields' => 'ids', 182 'update_post_meta_cache' => false, 183 'update_post_term_cache' => false, 184 'tax_query' => array( 185 array( 186 'taxonomy' => 'category', 187 'terms' => array( 'foo' ), 188 'field' => 'slug', 189 'operator' => 'IN', 190 ), 191 ), 192 ) 193 ); 170 194 171 195 $this->assertEquals( array( $p1 ), $q->posts ); … … 173 197 174 198 public function test_tax_query_single_query_single_term_operator_not_in() { 175 $t = self::factory()->term->create( array( 176 'taxonomy' => 'category', 177 'slug' => 'foo', 178 'name' => 'Foo', 179 ) ); 199 $t = self::factory()->term->create( 200 array( 201 'taxonomy' => 'category', 202 'slug' => 'foo', 203 'name' => 'Foo', 204 ) 205 ); 180 206 $p1 = self::factory()->post->create(); 181 207 $p2 = self::factory()->post->create(); … … 183 209 wp_set_post_terms( $p1, $t, 'category' ); 184 210 185 $q = new WP_Query( array( 186 'fields' => 'ids', 187 'update_post_meta_cache' => false, 188 'update_post_term_cache' => false, 189 'tax_query' => array( 190 array( 191 'taxonomy' => 'category', 192 'terms' => array( 'foo' ), 193 'field' => 'slug', 194 'operator' => 'NOT IN', 195 ), 196 ), 197 ) ); 211 $q = new WP_Query( 212 array( 213 'fields' => 'ids', 214 'update_post_meta_cache' => false, 215 'update_post_term_cache' => false, 216 'tax_query' => array( 217 array( 218 'taxonomy' => 'category', 219 'terms' => array( 'foo' ), 220 'field' => 'slug', 221 'operator' => 'NOT IN', 222 ), 223 ), 224 ) 225 ); 198 226 199 227 $this->assertEquals( array( $p2 ), $q->posts ); … … 201 229 202 230 public function test_tax_query_single_query_single_term_operator_and() { 203 $t = self::factory()->term->create( array( 204 'taxonomy' => 'category', 205 'slug' => 'foo', 206 'name' => 'Foo', 207 ) ); 231 $t = self::factory()->term->create( 232 array( 233 'taxonomy' => 'category', 234 'slug' => 'foo', 235 'name' => 'Foo', 236 ) 237 ); 208 238 $p1 = self::factory()->post->create(); 209 239 $p2 = self::factory()->post->create(); … … 211 241 wp_set_post_terms( $p1, $t, 'category' ); 212 242 213 $q = new WP_Query( array( 214 'fields' => 'ids', 215 'update_post_meta_cache' => false, 216 'update_post_term_cache' => false, 217 'tax_query' => array( 218 array( 219 'taxonomy' => 'category', 220 'terms' => array( 'foo' ), 221 'field' => 'slug', 222 'operator' => 'AND', 223 ), 224 ), 225 ) ); 243 $q = new WP_Query( 244 array( 245 'fields' => 'ids', 246 'update_post_meta_cache' => false, 247 'update_post_term_cache' => false, 248 'tax_query' => array( 249 array( 250 'taxonomy' => 'category', 251 'terms' => array( 'foo' ), 252 'field' => 'slug', 253 'operator' => 'AND', 254 ), 255 ), 256 ) 257 ); 226 258 227 259 $this->assertEquals( array( $p1 ), $q->posts ); … … 229 261 230 262 public function test_tax_query_single_query_multiple_terms_operator_in() { 231 $t1 = self::factory()->term->create( array( 232 'taxonomy' => 'category', 233 'slug' => 'foo', 234 'name' => 'Foo', 235 ) ); 236 $t2 = self::factory()->term->create( array( 237 'taxonomy' => 'category', 238 'slug' => 'bar', 239 'name' => 'Bar', 240 ) ); 263 $t1 = self::factory()->term->create( 264 array( 265 'taxonomy' => 'category', 266 'slug' => 'foo', 267 'name' => 'Foo', 268 ) 269 ); 270 $t2 = self::factory()->term->create( 271 array( 272 'taxonomy' => 'category', 273 'slug' => 'bar', 274 'name' => 'Bar', 275 ) 276 ); 241 277 $p1 = self::factory()->post->create(); 242 278 $p2 = self::factory()->post->create(); … … 246 282 wp_set_post_terms( $p2, $t2, 'category' ); 247 283 248 $q = new WP_Query( array( 249 'fields' => 'ids', 250 'update_post_meta_cache' => false, 251 'update_post_term_cache' => false, 252 'tax_query' => array( 253 array( 254 'taxonomy' => 'category', 255 'terms' => array( 'foo', 'bar' ), 256 'field' => 'slug', 257 'operator' => 'IN', 258 ), 259 ), 260 ) ); 284 $q = new WP_Query( 285 array( 286 'fields' => 'ids', 287 'update_post_meta_cache' => false, 288 'update_post_term_cache' => false, 289 'tax_query' => array( 290 array( 291 'taxonomy' => 'category', 292 'terms' => array( 'foo', 'bar' ), 293 'field' => 'slug', 294 'operator' => 'IN', 295 ), 296 ), 297 ) 298 ); 261 299 262 300 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); … … 264 302 265 303 public function test_tax_query_single_query_multiple_terms_operator_not_in() { 266 $t1 = self::factory()->term->create( array( 267 'taxonomy' => 'category', 268 'slug' => 'foo', 269 'name' => 'Foo', 270 ) ); 271 $t2 = self::factory()->term->create( array( 272 'taxonomy' => 'category', 273 'slug' => 'bar', 274 'name' => 'Bar', 275 ) ); 304 $t1 = self::factory()->term->create( 305 array( 306 'taxonomy' => 'category', 307 'slug' => 'foo', 308 'name' => 'Foo', 309 ) 310 ); 311 $t2 = self::factory()->term->create( 312 array( 313 'taxonomy' => 'category', 314 'slug' => 'bar', 315 'name' => 'Bar', 316 ) 317 ); 276 318 $p1 = self::factory()->post->create(); 277 319 $p2 = self::factory()->post->create(); … … 281 323 wp_set_post_terms( $p2, $t2, 'category' ); 282 324 283 $q = new WP_Query( array( 284 'fields' => 'ids', 285 'update_post_meta_cache' => false, 286 'update_post_term_cache' => false, 287 'tax_query' => array( 288 array( 289 'taxonomy' => 'category', 290 'terms' => array( 'foo', 'bar' ), 291 'field' => 'slug', 292 'operator' => 'NOT IN', 293 ), 294 ), 295 ) ); 325 $q = new WP_Query( 326 array( 327 'fields' => 'ids', 328 'update_post_meta_cache' => false, 329 'update_post_term_cache' => false, 330 'tax_query' => array( 331 array( 332 'taxonomy' => 'category', 333 'terms' => array( 'foo', 'bar' ), 334 'field' => 'slug', 335 'operator' => 'NOT IN', 336 ), 337 ), 338 ) 339 ); 296 340 297 341 $this->assertEquals( array( $p3 ), $q->posts ); … … 302 346 */ 303 347 public function test_tax_query_single_query_multiple_queries_operator_not_in() { 304 $t1 = self::factory()->term->create( array( 305 'taxonomy' => 'category', 306 'slug' => 'foo', 307 'name' => 'Foo', 308 ) ); 309 $t2 = self::factory()->term->create( array( 310 'taxonomy' => 'category', 311 'slug' => 'bar', 312 'name' => 'Bar', 313 ) ); 348 $t1 = self::factory()->term->create( 349 array( 350 'taxonomy' => 'category', 351 'slug' => 'foo', 352 'name' => 'Foo', 353 ) 354 ); 355 $t2 = self::factory()->term->create( 356 array( 357 'taxonomy' => 'category', 358 'slug' => 'bar', 359 'name' => 'Bar', 360 ) 361 ); 314 362 $p1 = self::factory()->post->create(); 315 363 $p2 = self::factory()->post->create(); … … 319 367 wp_set_post_terms( $p2, $t2, 'category' ); 320 368 321 $q = new WP_Query( array( 322 'fields' => 'ids', 323 'update_post_meta_cache' => false, 324 'update_post_term_cache' => false, 325 'tax_query' => array( 326 'relation' => 'AND', 327 array( 328 'taxonomy' => 'category', 329 'terms' => array( 'foo' ), 330 'field' => 'slug', 331 'operator' => 'NOT IN', 332 ), 333 array( 334 'taxonomy' => 'category', 335 'terms' => array( 'bar' ), 336 'field' => 'slug', 337 'operator' => 'NOT IN', 338 ), 339 ), 340 ) ); 369 $q = new WP_Query( 370 array( 371 'fields' => 'ids', 372 'update_post_meta_cache' => false, 373 'update_post_term_cache' => false, 374 'tax_query' => array( 375 'relation' => 'AND', 376 array( 377 'taxonomy' => 'category', 378 'terms' => array( 'foo' ), 379 'field' => 'slug', 380 'operator' => 'NOT IN', 381 ), 382 array( 383 'taxonomy' => 'category', 384 'terms' => array( 'bar' ), 385 'field' => 'slug', 386 'operator' => 'NOT IN', 387 ), 388 ), 389 ) 390 ); 341 391 342 392 $this->assertEquals( array( $p3 ), $q->posts ); … … 344 394 345 395 public function test_tax_query_single_query_multiple_terms_operator_and() { 346 $t1 = self::factory()->term->create( array( 347 'taxonomy' => 'category', 348 'slug' => 'foo', 349 'name' => 'Foo', 350 ) ); 351 $t2 = self::factory()->term->create( array( 352 'taxonomy' => 'category', 353 'slug' => 'bar', 354 'name' => 'Bar', 355 ) ); 396 $t1 = self::factory()->term->create( 397 array( 398 'taxonomy' => 'category', 399 'slug' => 'foo', 400 'name' => 'Foo', 401 ) 402 ); 403 $t2 = self::factory()->term->create( 404 array( 405 'taxonomy' => 'category', 406 'slug' => 'bar', 407 'name' => 'Bar', 408 ) 409 ); 356 410 $p1 = self::factory()->post->create(); 357 411 $p2 = self::factory()->post->create(); … … 361 415 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); 362 416 363 $q = new WP_Query( array( 364 'fields' => 'ids', 365 'update_post_meta_cache' => false, 366 'update_post_term_cache' => false, 367 'tax_query' => array( 368 array( 369 'taxonomy' => 'category', 370 'terms' => array( 'foo', 'bar' ), 371 'field' => 'slug', 372 'operator' => 'AND', 373 ), 374 ), 375 ) ); 417 $q = new WP_Query( 418 array( 419 'fields' => 'ids', 420 'update_post_meta_cache' => false, 421 'update_post_term_cache' => false, 422 'tax_query' => array( 423 array( 424 'taxonomy' => 'category', 425 'terms' => array( 'foo', 'bar' ), 426 'field' => 'slug', 427 'operator' => 'AND', 428 ), 429 ), 430 ) 431 ); 376 432 377 433 $this->assertEquals( array( $p2 ), $q->posts ); … … 395 451 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); 396 452 397 $q = new WP_Query( array( 398 'fields' => 'ids', 399 'orderby' => 'ID', 400 'order' => 'ASC', 401 'tax_query' => array( 402 array( 403 'taxonomy' => 'wptests_tax2', 404 'operator' => 'NOT EXISTS', 405 ), 406 ), 407 ) ); 453 $q = new WP_Query( 454 array( 455 'fields' => 'ids', 456 'orderby' => 'ID', 457 'order' => 'ASC', 458 'tax_query' => array( 459 array( 460 'taxonomy' => 'wptests_tax2', 461 'operator' => 'NOT EXISTS', 462 ), 463 ), 464 ) 465 ); 408 466 409 467 $this->assertEqualSets( array( $p1, $p3 ), $q->posts ); … … 431 489 wp_set_object_terms( $p3, array( $t3 ), 'wptests_tax2' ); 432 490 433 $q = new WP_Query( array( 434 'post_type' => 'wptests_cpt1', 435 'fields' => 'ids', 436 'tax_query' => array( 437 'relation' => 'OR', 438 array( 439 'taxonomy' => 'wptests_tax1', 440 'operator' => 'NOT EXISTS', 441 ), 442 array( 443 'taxonomy' => 'wptests_tax1', 444 'field' => 'slug', 445 'terms' => get_term_field( 'slug', $t1 ), 446 ), 447 ), 448 ) ); 491 $q = new WP_Query( 492 array( 493 'post_type' => 'wptests_cpt1', 494 'fields' => 'ids', 495 'tax_query' => array( 496 'relation' => 'OR', 497 array( 498 'taxonomy' => 'wptests_tax1', 499 'operator' => 'NOT EXISTS', 500 ), 501 array( 502 'taxonomy' => 'wptests_tax1', 503 'field' => 'slug', 504 'terms' => get_term_field( 'slug', $t1 ), 505 ), 506 ), 507 ) 508 ); 449 509 450 510 unregister_post_type( 'wptests_cpt1' ); … … 472 532 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); 473 533 474 $q = new WP_Query( array( 475 'fields' => 'ids', 476 'orderby' => 'ID', 477 'order' => 'ASC', 478 'tax_query' => array( 479 array( 480 'taxonomy' => 'wptests_tax2', 481 'operator' => 'EXISTS', 482 ), 483 ), 484 ) ); 534 $q = new WP_Query( 535 array( 536 'fields' => 'ids', 537 'orderby' => 'ID', 538 'order' => 'ASC', 539 'tax_query' => array( 540 array( 541 'taxonomy' => 'wptests_tax2', 542 'operator' => 'EXISTS', 543 ), 544 ), 545 ) 546 ); 485 547 486 548 $this->assertEqualSets( array( $p2 ), $q->posts ); … … 504 566 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); 505 567 506 $q = new WP_Query( array( 507 'fields' => 'ids', 508 'orderby' => 'ID', 509 'order' => 'ASC', 510 'tax_query' => array( 511 array( 512 'taxonomy' => 'wptests_tax2', 513 'operator' => 'EXISTS', 514 'terms' => array( 'foo', 'bar' ), 515 ), 516 ), 517 ) ); 568 $q = new WP_Query( 569 array( 570 'fields' => 'ids', 571 'orderby' => 'ID', 572 'order' => 'ASC', 573 'tax_query' => array( 574 array( 575 'taxonomy' => 'wptests_tax2', 576 'operator' => 'EXISTS', 577 'terms' => array( 'foo', 'bar' ), 578 ), 579 ), 580 ) 581 ); 518 582 519 583 $this->assertEqualSets( array( $p2 ), $q->posts ); … … 537 601 wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); 538 602 539 $q = new WP_Query( array( 540 'fields' => 'ids', 541 'orderby' => 'ID', 542 'order' => 'ASC', 543 'tax_query' => array( 544 array( 545 'operator' => 'EXISTS', 546 ), 547 ), 548 ) ); 603 $q = new WP_Query( 604 array( 605 'fields' => 'ids', 606 'orderby' => 'ID', 607 'order' => 'ASC', 608 'tax_query' => array( 609 array( 610 'operator' => 'EXISTS', 611 ), 612 ), 613 ) 614 ); 549 615 550 616 $this->assertEmpty( $q->posts ); … … 552 618 553 619 public function test_tax_query_multiple_queries_relation_and() { 554 $t1 = self::factory()->term->create( array( 555 'taxonomy' => 'category', 556 'slug' => 'foo', 557 'name' => 'Foo', 558 ) ); 559 $t2 = self::factory()->term->create( array( 560 'taxonomy' => 'category', 561 'slug' => 'bar', 562 'name' => 'Bar', 563 ) ); 620 $t1 = self::factory()->term->create( 621 array( 622 'taxonomy' => 'category', 623 'slug' => 'foo', 624 'name' => 'Foo', 625 ) 626 ); 627 $t2 = self::factory()->term->create( 628 array( 629 'taxonomy' => 'category', 630 'slug' => 'bar', 631 'name' => 'Bar', 632 ) 633 ); 564 634 $p1 = self::factory()->post->create(); 565 635 $p2 = self::factory()->post->create(); … … 569 639 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); 570 640 571 $q = new WP_Query( array( 572 'fields' => 'ids', 573 'update_post_meta_cache' => false, 574 'update_post_term_cache' => false, 575 'tax_query' => array( 576 'relation' => 'AND', 577 array( 578 'taxonomy' => 'category', 579 'terms' => array( 'foo' ), 580 'field' => 'slug', 581 ), 582 array( 583 'taxonomy' => 'category', 584 'terms' => array( 'bar' ), 585 'field' => 'slug', 586 ), 587 ), 588 ) ); 641 $q = new WP_Query( 642 array( 643 'fields' => 'ids', 644 'update_post_meta_cache' => false, 645 'update_post_term_cache' => false, 646 'tax_query' => array( 647 'relation' => 'AND', 648 array( 649 'taxonomy' => 'category', 650 'terms' => array( 'foo' ), 651 'field' => 'slug', 652 ), 653 array( 654 'taxonomy' => 'category', 655 'terms' => array( 'bar' ), 656 'field' => 'slug', 657 ), 658 ), 659 ) 660 ); 589 661 590 662 $this->assertEquals( array( $p2 ), $q->posts ); … … 592 664 593 665 public function test_tax_query_multiple_queries_relation_or() { 594 $t1 = self::factory()->term->create( array( 595 'taxonomy' => 'category', 596 'slug' => 'foo', 597 'name' => 'Foo', 598 ) ); 599 $t2 = self::factory()->term->create( array( 600 'taxonomy' => 'category', 601 'slug' => 'bar', 602 'name' => 'Bar', 603 ) ); 666 $t1 = self::factory()->term->create( 667 array( 668 'taxonomy' => 'category', 669 'slug' => 'foo', 670 'name' => 'Foo', 671 ) 672 ); 673 $t2 = self::factory()->term->create( 674 array( 675 'taxonomy' => 'category', 676 'slug' => 'bar', 677 'name' => 'Bar', 678 ) 679 ); 604 680 $p1 = self::factory()->post->create(); 605 681 $p2 = self::factory()->post->create(); … … 609 685 wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); 610 686 611 $q = new WP_Query( array( 612 'fields' => 'ids', 613 'update_post_meta_cache' => false, 614 'update_post_term_cache' => false, 615 'tax_query' => array( 616 'relation' => 'OR', 617 array( 618 'taxonomy' => 'category', 619 'terms' => array( 'foo' ), 620 'field' => 'slug', 621 ), 622 array( 623 'taxonomy' => 'category', 624 'terms' => array( 'bar' ), 625 'field' => 'slug', 626 ), 627 ), 628 ) ); 687 $q = new WP_Query( 688 array( 689 'fields' => 'ids', 690 'update_post_meta_cache' => false, 691 'update_post_term_cache' => false, 692 'tax_query' => array( 693 'relation' => 'OR', 694 array( 695 'taxonomy' => 'category', 696 'terms' => array( 'foo' ), 697 'field' => 'slug', 698 ), 699 array( 700 'taxonomy' => 'category', 701 'terms' => array( 'bar' ), 702 'field' => 'slug', 703 ), 704 ), 705 ) 706 ); 629 707 630 708 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); … … 632 710 633 711 public function test_tax_query_multiple_queries_different_taxonomies() { 634 $t1 = self::factory()->term->create( array( 635 'taxonomy' => 'post_tag', 636 'slug' => 'foo', 637 'name' => 'Foo', 638 ) ); 639 $t2 = self::factory()->term->create( array( 640 'taxonomy' => 'category', 641 'slug' => 'bar', 642 'name' => 'Bar', 643 ) ); 712 $t1 = self::factory()->term->create( 713 array( 714 'taxonomy' => 'post_tag', 715 'slug' => 'foo', 716 'name' => 'Foo', 717 ) 718 ); 719 $t2 = self::factory()->term->create( 720 array( 721 'taxonomy' => 'category', 722 'slug' => 'bar', 723 'name' => 'Bar', 724 ) 725 ); 644 726 $p1 = self::factory()->post->create(); 645 727 $p2 = self::factory()->post->create(); … … 649 731 wp_set_object_terms( $p2, $t2, 'category' ); 650 732 651 $q = new WP_Query( array( 652 'fields' => 'ids', 653 'update_post_meta_cache' => false, 654 'update_post_term_cache' => false, 655 'tax_query' => array( 656 'relation' => 'OR', 657 array( 658 'taxonomy' => 'post_tag', 659 'terms' => array( 'foo' ), 660 'field' => 'slug', 661 ), 662 array( 663 'taxonomy' => 'category', 664 'terms' => array( 'bar' ), 665 'field' => 'slug', 666 ), 667 ), 668 ) ); 733 $q = new WP_Query( 734 array( 735 'fields' => 'ids', 736 'update_post_meta_cache' => false, 737 'update_post_term_cache' => false, 738 'tax_query' => array( 739 'relation' => 'OR', 740 array( 741 'taxonomy' => 'post_tag', 742 'terms' => array( 'foo' ), 743 'field' => 'slug', 744 ), 745 array( 746 'taxonomy' => 'category', 747 'terms' => array( 'bar' ), 748 'field' => 'slug', 749 ), 750 ), 751 ) 752 ); 669 753 670 754 $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); … … 678 762 register_taxonomy( 'bar', 'post' ); 679 763 680 $foo_term_1 = self::factory()->term->create( array( 681 'taxonomy' => 'foo', 682 ) ); 683 $foo_term_2 = self::factory()->term->create( array( 684 'taxonomy' => 'foo', 685 ) ); 686 $bar_term_1 = self::factory()->term->create( array( 687 'taxonomy' => 'bar', 688 ) ); 689 $bar_term_2 = self::factory()->term->create( array( 690 'taxonomy' => 'bar', 691 ) ); 764 $foo_term_1 = self::factory()->term->create( 765 array( 766 'taxonomy' => 'foo', 767 ) 768 ); 769 $foo_term_2 = self::factory()->term->create( 770 array( 771 'taxonomy' => 'foo', 772 ) 773 ); 774 $bar_term_1 = self::factory()->term->create( 775 array( 776 'taxonomy' => 'bar', 777 ) 778 ); 779 $bar_term_2 = self::factory()->term->create( 780 array( 781 'taxonomy' => 'bar', 782 ) 783 ); 692 784 693 785 $p1 = self::factory()->post->create(); … … 702 794 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); 703 795 704 $q = new WP_Query( array( 705 'fields' => 'ids', 706 'update_post_meta_cache' => false, 707 'update_post_term_cache' => false, 708 'tax_query' => array( 709 'relation' => 'OR', 710 array( 711 'relation' => 'AND', 712 array( 713 'taxonomy' => 'foo', 714 'terms' => array( $foo_term_1 ), 715 'field' => 'term_id', 716 ), 717 array( 718 'taxonomy' => 'bar', 719 'terms' => array( $bar_term_1 ), 720 'field' => 'term_id', 721 ), 722 ), 723 array( 724 'relation' => 'AND', 725 array( 726 'taxonomy' => 'foo', 727 'terms' => array( $foo_term_2 ), 728 'field' => 'term_id', 729 ), 730 array( 731 'taxonomy' => 'bar', 732 'terms' => array( $bar_term_2 ), 733 'field' => 'term_id', 734 ), 735 ), 736 ), 737 ) ); 796 $q = new WP_Query( 797 array( 798 'fields' => 'ids', 799 'update_post_meta_cache' => false, 800 'update_post_term_cache' => false, 801 'tax_query' => array( 802 'relation' => 'OR', 803 array( 804 'relation' => 'AND', 805 array( 806 'taxonomy' => 'foo', 807 'terms' => array( $foo_term_1 ), 808 'field' => 'term_id', 809 ), 810 array( 811 'taxonomy' => 'bar', 812 'terms' => array( $bar_term_1 ), 813 'field' => 'term_id', 814 ), 815 ), 816 array( 817 'relation' => 'AND', 818 array( 819 'taxonomy' => 'foo', 820 'terms' => array( $foo_term_2 ), 821 'field' => 'term_id', 822 ), 823 array( 824 'taxonomy' => 'bar', 825 'terms' => array( $bar_term_2 ), 826 'field' => 'term_id', 827 ), 828 ), 829 ), 830 ) 831 ); 738 832 739 833 _unregister_taxonomy( 'foo' ); … … 750 844 register_taxonomy( 'bar', 'post' ); 751 845 752 $foo_term_1 = self::factory()->term->create( array( 753 'taxonomy' => 'foo', 754 ) ); 755 $foo_term_2 = self::factory()->term->create( array( 756 'taxonomy' => 'foo', 757 ) ); 758 $bar_term_1 = self::factory()->term->create( array( 759 'taxonomy' => 'bar', 760 ) ); 761 $bar_term_2 = self::factory()->term->create( array( 762 'taxonomy' => 'bar', 763 ) ); 846 $foo_term_1 = self::factory()->term->create( 847 array( 848 'taxonomy' => 'foo', 849 ) 850 ); 851 $foo_term_2 = self::factory()->term->create( 852 array( 853 'taxonomy' => 'foo', 854 ) 855 ); 856 $bar_term_1 = self::factory()->term->create( 857 array( 858 'taxonomy' => 'bar', 859 ) 860 ); 861 $bar_term_2 = self::factory()->term->create( 862 array( 863 'taxonomy' => 'bar', 864 ) 865 ); 764 866 765 867 $p1 = self::factory()->post->create(); … … 774 876 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); 775 877 776 $q = new WP_Query( array( 777 'fields' => 'ids', 778 'update_post_meta_cache' => false, 779 'update_post_term_cache' => false, 780 'tax_query' => array( 781 'relation' => 'OR', 782 array( 783 'taxonomy' => 'foo', 784 'terms' => array( $foo_term_2 ), 785 'field' => 'term_id', 786 ), 787 array( 788 'relation' => 'AND', 878 $q = new WP_Query( 879 array( 880 'fields' => 'ids', 881 'update_post_meta_cache' => false, 882 'update_post_term_cache' => false, 883 'tax_query' => array( 884 'relation' => 'OR', 789 885 array( 790 886 'taxonomy' => 'foo', 791 'terms' => array( $foo_term_1 ), 792 'field' => 'term_id', 793 ), 794 array( 795 'taxonomy' => 'bar', 796 'terms' => array( $bar_term_1 ), 797 'field' => 'term_id', 798 ), 799 ), 800 ), 801 ) ); 887 'terms' => array( $foo_term_2 ), 888 'field' => 'term_id', 889 ), 890 array( 891 'relation' => 'AND', 892 array( 893 'taxonomy' => 'foo', 894 'terms' => array( $foo_term_1 ), 895 'field' => 'term_id', 896 ), 897 array( 898 'taxonomy' => 'bar', 899 'terms' => array( $bar_term_1 ), 900 'field' => 'term_id', 901 ), 902 ), 903 ), 904 ) 905 ); 802 906 803 907 _unregister_taxonomy( 'foo' ); … … 814 918 register_taxonomy( 'bar', 'post' ); 815 919 816 $foo_term_1 = self::factory()->term->create( array( 817 'taxonomy' => 'foo', 818 ) ); 819 $foo_term_2 = self::factory()->term->create( array( 820 'taxonomy' => 'foo', 821 ) ); 822 $bar_term_1 = self::factory()->term->create( array( 823 'taxonomy' => 'bar', 824 ) ); 825 $bar_term_2 = self::factory()->term->create( array( 826 'taxonomy' => 'bar', 827 ) ); 920 $foo_term_1 = self::factory()->term->create( 921 array( 922 'taxonomy' => 'foo', 923 ) 924 ); 925 $foo_term_2 = self::factory()->term->create( 926 array( 927 'taxonomy' => 'foo', 928 ) 929 ); 930 $bar_term_1 = self::factory()->term->create( 931 array( 932 'taxonomy' => 'bar', 933 ) 934 ); 935 $bar_term_2 = self::factory()->term->create( 936 array( 937 'taxonomy' => 'bar', 938 ) 939 ); 828 940 829 941 $p1 = self::factory()->post->create(); … … 839 951 wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); 840 952 841 $q = new WP_Query( array( 842 'fields' => 'ids', 843 'update_post_meta_cache' => false, 844 'update_post_term_cache' => false, 845 'tax_query' => array( 846 'relation' => 'OR', 847 array( 848 'taxonomy' => 'foo', 849 'terms' => array( $foo_term_2 ), 850 'field' => 'term_id', 851 ), 852 array( 853 'relation' => 'AND', 953 $q = new WP_Query( 954 array( 955 'fields' => 'ids', 956 'update_post_meta_cache' => false, 957 'update_post_term_cache' => false, 958 'tax_query' => array( 959 'relation' => 'OR', 854 960 array( 855 961 'taxonomy' => 'foo', 856 'terms' => array( $foo_term_1),857 'field' => 'term_id',858 ), 859 array( 860 'relation' => ' OR',962 'terms' => array( $foo_term_2 ), 963 'field' => 'term_id', 964 ), 965 array( 966 'relation' => 'AND', 861 967 array( 862 'taxonomy' => ' bar',863 'terms' => array( $bar_term_1 ),864 'field' => 'term_id',968 'taxonomy' => 'foo', 969 'terms' => array( $foo_term_1 ), 970 'field' => 'term_id', 865 971 ), 866 972 array( 867 'taxonomy' => 'bar', 868 'terms' => array( $bar_term_2 ), 869 'field' => 'term_id', 973 'relation' => 'OR', 974 array( 975 'taxonomy' => 'bar', 976 'terms' => array( $bar_term_1 ), 977 'field' => 'term_id', 978 ), 979 array( 980 'taxonomy' => 'bar', 981 'terms' => array( $bar_term_2 ), 982 'field' => 'term_id', 983 ), 870 984 ), 871 985 ), 872 986 ), 873 ) ,874 ) );987 ) 988 ); 875 989 876 990 _unregister_taxonomy( 'foo' ); … … 888 1002 self::factory()->post->create_many( 2 ); 889 1003 890 $query = new WP_Query( array( 891 'fields' => 'ids', 892 'update_post_term_cache' => false, 893 'update_post_meta_cache' => false, 894 'tax_query' => array( 895 'relation' => 'OR', 896 array( 897 'taxonomy' => 'post_tag', 898 'field' => 'id', 899 'terms' => false, 900 'operator' => 'IN' 901 ), 902 array( 903 'taxonomy' => 'category', 904 'field' => 'id', 905 'terms' => false, 906 'operator' => 'IN' 907 ), 908 ) 909 ) ); 1004 $query = new WP_Query( 1005 array( 1006 'fields' => 'ids', 1007 'update_post_term_cache' => false, 1008 'update_post_meta_cache' => false, 1009 'tax_query' => array( 1010 'relation' => 'OR', 1011 array( 1012 'taxonomy' => 'post_tag', 1013 'field' => 'id', 1014 'terms' => false, 1015 'operator' => 'IN', 1016 ), 1017 array( 1018 'taxonomy' => 'category', 1019 'field' => 'id', 1020 'terms' => false, 1021 'operator' => 'IN', 1022 ), 1023 ), 1024 ) 1025 ); 910 1026 911 1027 $posts = $query->get_posts(); 912 $this->assertEquals( 0 1028 $this->assertEquals( 0, count( $posts ) ); 913 1029 } 914 1030 … … 921 1037 self::factory()->post->create_many( 2 ); 922 1038 923 $query = new WP_Query( array( 924 'fields' => 'ids', 925 'update_post_term_cache' => false, 926 'update_post_meta_cache' => false, 927 'tax_query' => array( 928 'relation' => 'OR', 929 array( 930 'taxonomy' => 'post_tag', 931 'field' => 'id', 932 'terms' => array( 'foo' ), 933 'operator' => 'IN' 934 ), 935 array( 936 'taxonomy' => 'category', 937 'field' => 'id', 938 'terms' => false, 939 'operator' => 'IN' 940 ), 941 ) 942 ) ); 1039 $query = new WP_Query( 1040 array( 1041 'fields' => 'ids', 1042 'update_post_term_cache' => false, 1043 'update_post_meta_cache' => false, 1044 'tax_query' => array( 1045 'relation' => 'OR', 1046 array( 1047 'taxonomy' => 'post_tag', 1048 'field' => 'id', 1049 'terms' => array( 'foo' ), 1050 'operator' => 'IN', 1051 ), 1052 array( 1053 'taxonomy' => 'category', 1054 'field' => 'id', 1055 'terms' => false, 1056 'operator' => 'IN', 1057 ), 1058 ), 1059 ) 1060 ); 943 1061 944 1062 $posts = $query->get_posts(); 945 $this->assertEquals( 0 1063 $this->assertEquals( 0, count( $posts ) ); 946 1064 } 947 1065 948 1066 public function test_tax_query_include_children() { 949 $cat_a = self::factory()->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) ); 950 $cat_b = self::factory()->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) ); 951 $cat_c = self::factory()->term->create( array( 'taxonomy' => 'category', 'name' => 'East Syndney', 'parent' => $cat_b ) ); 952 $cat_d = self::factory()->term->create( array( 'taxonomy' => 'category', 'name' => 'West Syndney', 'parent' => $cat_b ) ); 1067 $cat_a = self::factory()->term->create( 1068 array( 1069 'taxonomy' => 'category', 1070 'name' => 'Australia', 1071 ) 1072 ); 1073 $cat_b = self::factory()->term->create( 1074 array( 1075 'taxonomy' => 'category', 1076 'name' => 'Sydney', 1077 'parent' => $cat_a, 1078 ) 1079 ); 1080 $cat_c = self::factory()->term->create( 1081 array( 1082 'taxonomy' => 'category', 1083 'name' => 'East Syndney', 1084 'parent' => $cat_b, 1085 ) 1086 ); 1087 $cat_d = self::factory()->term->create( 1088 array( 1089 'taxonomy' => 'category', 1090 'name' => 'West Syndney', 1091 'parent' => $cat_b, 1092 ) 1093 ); 953 1094 954 1095 $post_a = self::factory()->post->create( array( 'post_category' => array( $cat_a ) ) ); … … 957 1098 $post_d = self::factory()->post->create( array( 'post_category' => array( $cat_d ) ) ); 958 1099 959 $posts = get_posts( array( 960 'fields' => 'ids', 961 'update_post_meta_cache' => false, 962 'update_post_term_cache' => false, 963 'tax_query' => array( 964 array( 965 'taxonomy' => 'category', 966 'field' => 'id', 967 'terms' => array( $cat_a ), 968 ) 969 ) 970 ) ); 971 972 $this->assertEquals( 4 , count( $posts ) ); 973 974 $posts = get_posts( array( 975 'fields' => 'ids', 976 'update_post_meta_cache' => false, 977 'update_post_term_cache' => false, 978 'tax_query' => array( 979 array( 980 'taxonomy' => 'category', 981 'field' => 'id', 982 'terms' => array( $cat_a ), 983 'include_children' => false 984 ) 985 ) 986 ) ); 987 988 $this->assertEquals( 1 , count( $posts ) ); 989 990 $posts = get_posts( array( 991 'fields' => 'ids', 992 'update_post_meta_cache' => false, 993 'update_post_term_cache' => false, 994 'tax_query' => array( 995 array( 996 'taxonomy' => 'category', 997 'field' => 'id', 998 'terms' => array( $cat_b ), 999 ) 1000 ) 1001 ) ); 1002 1003 $this->assertEquals( 3 , count( $posts ) ); 1004 1005 $posts = get_posts( array( 1006 'fields' => 'ids', 1007 'update_post_meta_cache' => false, 1008 'update_post_term_cache' => false, 1009 'tax_query' => array( 1010 array( 1011 'taxonomy' => 'category', 1012 'field' => 'id', 1013 'terms' => array( $cat_b ), 1014 'include_children' => false 1015 ) 1016 ) 1017 ) ); 1018 1019 $this->assertEquals( 1 , count( $posts ) ); 1020 1021 $posts = get_posts( array( 1022 'fields' => 'ids', 1023 'update_post_meta_cache' => false, 1024 'update_post_term_cache' => false, 1025 'tax_query' => array( 1026 array( 1027 'taxonomy' => 'category', 1028 'field' => 'id', 1029 'terms' => array( $cat_c ), 1030 ) 1031 ) 1032 ) ); 1033 1034 $this->assertEquals( 1 , count( $posts ) ); 1035 1036 $posts = get_posts( array( 1037 'fields' => 'ids', 1038 'update_post_meta_cache' => false, 1039 'update_post_term_cache' => false, 1040 'tax_query' => array( 1041 array( 1042 'taxonomy' => 'category', 1043 'field' => 'id', 1044 'terms' => array( $cat_c ), 1045 'include_children' => false 1046 ) 1047 ) 1048 ) ); 1049 1050 $this->assertEquals( 1 , count( $posts ) ); 1100 $posts = get_posts( 1101 array( 1102 'fields' => 'ids', 1103 'update_post_meta_cache' => false, 1104 'update_post_term_cache' => false, 1105 'tax_query' => array( 1106 array( 1107 'taxonomy' => 'category', 1108 'field' => 'id', 1109 'terms' => array( $cat_a ), 1110 ), 1111 ), 1112 ) 1113 ); 1114 1115 $this->assertEquals( 4, count( $posts ) ); 1116 1117 $posts = get_posts( 1118 array( 1119 'fields' => 'ids', 1120 'update_post_meta_cache' => false, 1121 'update_post_term_cache' => false, 1122 'tax_query' => array( 1123 array( 1124 'taxonomy' => 'category', 1125 'field' => 'id', 1126 'terms' => array( $cat_a ), 1127 'include_children' => false, 1128 ), 1129 ), 1130 ) 1131 ); 1132 1133 $this->assertEquals( 1, count( $posts ) ); 1134 1135 $posts = get_posts( 1136 array( 1137 'fields' => 'ids', 1138 'update_post_meta_cache' => false, 1139 'update_post_term_cache' => false, 1140 'tax_query' => array( 1141 array( 1142 'taxonomy' => 'category', 1143 'field' => 'id', 1144 'terms' => array( $cat_b ), 1145 ), 1146 ), 1147 ) 1148 ); 1149 1150 $this->assertEquals( 3, count( $posts ) ); 1151 1152 $posts = get_posts( 1153 array( 1154 'fields' => 'ids', 1155 'update_post_meta_cache' => false, 1156 'update_post_term_cache' => false, 1157 'tax_query' => array( 1158 array( 1159 'taxonomy' => 'category', 1160 'field' => 'id', 1161 'terms' => array( $cat_b ), 1162 'include_children' => false, 1163 ), 1164 ), 1165 ) 1166 ); 1167 1168 $this->assertEquals( 1, count( $posts ) ); 1169 1170 $posts = get_posts( 1171 array( 1172 'fields' => 'ids', 1173 'update_post_meta_cache' => false, 1174 'update_post_term_cache' => false, 1175 'tax_query' => array( 1176 array( 1177 'taxonomy' => 'category', 1178 'field' => 'id', 1179 'terms' => array( $cat_c ), 1180 ), 1181 ), 1182 ) 1183 ); 1184 1185 $this->assertEquals( 1, count( $posts ) ); 1186 1187 $posts = get_posts( 1188 array( 1189 'fields' => 'ids', 1190 'update_post_meta_cache' => false, 1191 'update_post_term_cache' => false, 1192 'tax_query' => array( 1193 array( 1194 'taxonomy' => 'category', 1195 'field' => 'id', 1196 'terms' => array( $cat_c ), 1197 'include_children' => false, 1198 ), 1199 ), 1200 ) 1201 ); 1202 1203 $this->assertEquals( 1, count( $posts ) ); 1051 1204 } 1052 1205 … … 1055 1208 1056 1209 register_taxonomy_for_object_type( 'post_tag', 'attachment:image' ); 1057 $tag_id = self::factory()->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) ); 1058 $image_id = self::factory()->attachment->create_object( 'image.jpg', 0, array( 1059 'post_mime_type' => 'image/jpeg', 1060 'post_type' => 'attachment' 1061 ) ); 1210 $tag_id = self::factory()->term->create( 1211 array( 1212 'slug' => rand_str(), 1213 'name' => rand_str(), 1214 ) 1215 ); 1216 $image_id = self::factory()->attachment->create_object( 1217 'image.jpg', 0, array( 1218 'post_mime_type' => 'image/jpeg', 1219 'post_type' => 'attachment', 1220 ) 1221 ); 1062 1222 wp_set_object_terms( $image_id, $tag_id, 'post_tag' ); 1063 1223 1064 $posts = $q->query( array( 1065 'fields' => 'ids', 1066 'update_post_meta_cache' => false, 1067 'update_post_term_cache' => false, 1068 'post_type' => 'attachment', 1069 'post_status' => 'inherit', 1070 'tax_query' => array( 1071 array( 1072 'taxonomy' => 'post_tag', 1073 'field' => 'term_id', 1074 'terms' => array( $tag_id ) 1075 ) 1076 ) 1077 ) ); 1224 $posts = $q->query( 1225 array( 1226 'fields' => 'ids', 1227 'update_post_meta_cache' => false, 1228 'update_post_term_cache' => false, 1229 'post_type' => 'attachment', 1230 'post_status' => 'inherit', 1231 'tax_query' => array( 1232 array( 1233 'taxonomy' => 'post_tag', 1234 'field' => 'term_id', 1235 'terms' => array( $tag_id ), 1236 ), 1237 ), 1238 ) 1239 ); 1078 1240 1079 1241 $this->assertEquals( array( $image_id ), $posts ); … … 1082 1244 public function test_tax_query_no_taxonomy() { 1083 1245 $cat_id = self::factory()->category->create( array( 'name' => 'alpha' ) ); 1084 self::factory()->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) ); 1085 1086 $response1 = new WP_Query( array( 1087 'tax_query' => array( 1088 array( 'terms' => array( $cat_id ) ) 1089 ) 1090 ) ); 1246 self::factory()->post->create( 1247 array( 1248 'post_title' => 'alpha', 1249 'post_category' => array( $cat_id ), 1250 ) 1251 ); 1252 1253 $response1 = new WP_Query( 1254 array( 1255 'tax_query' => array( 1256 array( 'terms' => array( $cat_id ) ), 1257 ), 1258 ) 1259 ); 1091 1260 $this->assertEmpty( $response1->posts ); 1092 1261 1093 $response2 = new WP_Query( array( 1094 'fields' => 'ids', 1095 'update_post_meta_cache' => false, 1096 'update_post_term_cache' => false, 1097 'tax_query' => array( 1098 array( 1099 'taxonomy' => 'category', 1100 'terms' => array( $cat_id ) 1101 ) 1102 ) 1103 ) ); 1262 $response2 = new WP_Query( 1263 array( 1264 'fields' => 'ids', 1265 'update_post_meta_cache' => false, 1266 'update_post_term_cache' => false, 1267 'tax_query' => array( 1268 array( 1269 'taxonomy' => 'category', 1270 'terms' => array( $cat_id ), 1271 ), 1272 ), 1273 ) 1274 ); 1104 1275 $this->assertNotEmpty( $response2->posts ); 1105 1276 1106 $term = get_category( $cat_id ); 1107 $response3 = new WP_Query( array( 1108 'fields' => 'ids', 1109 'update_post_meta_cache' => false, 1110 'update_post_term_cache' => false, 1111 'tax_query' => array( 1112 array( 1113 'field' => 'term_taxonomy_id', 1114 'terms' => array( $term->term_taxonomy_id ) 1115 ) 1116 ) 1117 ) ); 1277 $term = get_category( $cat_id ); 1278 $response3 = new WP_Query( 1279 array( 1280 'fields' => 'ids', 1281 'update_post_meta_cache' => false, 1282 'update_post_term_cache' => false, 1283 'tax_query' => array( 1284 array( 1285 'field' => 'term_taxonomy_id', 1286 'terms' => array( $term->term_taxonomy_id ), 1287 ), 1288 ), 1289 ) 1290 ); 1118 1291 $this->assertNotEmpty( $response3->posts ); 1119 1292 } … … 1128 1301 // need term_taxonomy_ids in addition to term_ids, so no factory 1129 1302 for ( $i = 0; $i < 5; $i++ ) { 1130 $cats[ $i] = wp_insert_term( 'category-' . $i, 'category' );1131 $tags[ $i] = wp_insert_term( 'tag-' . $i, 'post_tag' );1303 $cats[ $i ] = wp_insert_term( 'category-' . $i, 'category' ); 1304 $tags[ $i ] = wp_insert_term( 'tag-' . $i, 'post_tag' ); 1132 1305 1133 1306 // post 0 gets all terms 1134 wp_set_object_terms( $posts[0], array( $cats[ $i]['term_id'] ), 'category', true );1135 wp_set_object_terms( $posts[0], array( $tags[ $i]['term_id'] ), 'post_tag', true );1307 wp_set_object_terms( $posts[0], array( $cats[ $i ]['term_id'] ), 'category', true ); 1308 wp_set_object_terms( $posts[0], array( $tags[ $i ]['term_id'] ), 'post_tag', true ); 1136 1309 } 1137 1310 … … 1145 1318 wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); 1146 1319 1147 $results1 = $q->query( array( 1148 'fields' => 'ids', 1149 'update_post_meta_cache' => false, 1150 'update_post_term_cache' => false, 1151 'orderby' => 'ID', 1152 'order' => 'ASC', 1153 'tax_query' => array( 1154 'relation' => 'OR', 1155 array( 1156 'field' => 'term_taxonomy_id', 1157 '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'] ), 1158 'operator' => 'AND', 1159 'include_children' => false, 1160 ), 1161 array( 1162 'field' => 'term_taxonomy_id', 1163 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), 1164 'operator' => 'AND', 1165 'include_children' => false, 1166 ) 1167 ) 1168 ) ); 1320 $results1 = $q->query( 1321 array( 1322 'fields' => 'ids', 1323 'update_post_meta_cache' => false, 1324 'update_post_term_cache' => false, 1325 'orderby' => 'ID', 1326 'order' => 'ASC', 1327 'tax_query' => array( 1328 'relation' => 'OR', 1329 array( 1330 'field' => 'term_taxonomy_id', 1331 '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'] ), 1332 'operator' => 'AND', 1333 'include_children' => false, 1334 ), 1335 array( 1336 'field' => 'term_taxonomy_id', 1337 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), 1338 'operator' => 'AND', 1339 'include_children' => false, 1340 ), 1341 ), 1342 ) 1343 ); 1169 1344 1170 1345 $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' ); 1171 1346 1172 $results2 = $q->query( array( 1173 'fields' => 'ids', 1174 'update_post_meta_cache' => false, 1175 'update_post_term_cache' => false, 1176 'orderby' => 'ID', 1177 'order' => 'ASC', 1178 'tax_query' => array( 1179 'relation' => 'AND', 1180 array( 1181 'field' => 'term_taxonomy_id', 1182 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ), 1183 'operator' => 'IN', 1184 'include_children' => false, 1185 ), 1186 array( 1187 'field' => 'term_taxonomy_id', 1188 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), 1189 'operator' => 'IN', 1190 'include_children' => false, 1191 ) 1192 ) 1193 ) ); 1347 $results2 = $q->query( 1348 array( 1349 'fields' => 'ids', 1350 'update_post_meta_cache' => false, 1351 'update_post_term_cache' => false, 1352 'orderby' => 'ID', 1353 'order' => 'ASC', 1354 'tax_query' => array( 1355 'relation' => 'AND', 1356 array( 1357 'field' => 'term_taxonomy_id', 1358 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ), 1359 'operator' => 'IN', 1360 'include_children' => false, 1361 ), 1362 array( 1363 'field' => 'term_taxonomy_id', 1364 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), 1365 'operator' => 'IN', 1366 'include_children' => false, 1367 ), 1368 ), 1369 ) 1370 ); 1194 1371 1195 1372 $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' ); … … 1201 1378 public function test_populate_taxonomy_query_var_from_tax_query() { 1202 1379 register_taxonomy( 'foo', 'post' ); 1203 $t = self::factory()->term->create( array( 1204 'taxonomy' => 'foo', 1205 ) ); 1206 $c = self::factory()->term->create( array( 1207 'taxonomy' => 'category', 1208 ) ); 1209 1210 $q = new WP_Query( array( 1211 'tax_query' => array( 1212 // Empty terms mean that this one should be skipped 1213 array( 1214 'taxonomy' => 'bar', 1215 'terms' => array(), 1216 ), 1217 1218 // Category and post tags should be skipped 1219 array( 1220 'taxonomy' => 'category', 1221 'terms' => array( $c ), 1222 ), 1223 1224 array( 1225 'taxonomy' => 'foo', 1226 'terms' => array( $t ), 1227 ), 1228 ), 1229 ) ); 1380 $t = self::factory()->term->create( 1381 array( 1382 'taxonomy' => 'foo', 1383 ) 1384 ); 1385 $c = self::factory()->term->create( 1386 array( 1387 'taxonomy' => 'category', 1388 ) 1389 ); 1390 1391 $q = new WP_Query( 1392 array( 1393 'tax_query' => array( 1394 // Empty terms mean that this one should be skipped 1395 array( 1396 'taxonomy' => 'bar', 1397 'terms' => array(), 1398 ), 1399 1400 // Category and post tags should be skipped 1401 array( 1402 'taxonomy' => 'category', 1403 'terms' => array( $c ), 1404 ), 1405 1406 array( 1407 'taxonomy' => 'foo', 1408 'terms' => array( $t ), 1409 ), 1410 ), 1411 ) 1412 ); 1230 1413 1231 1414 $this->assertSame( 'foo', $q->get( 'taxonomy' ) ); … … 1237 1420 register_taxonomy( 'foo', 'post' ); 1238 1421 register_taxonomy( 'foo1', 'post' ); 1239 $t = self::factory()->term->create( array( 1240 'taxonomy' => 'foo', 1241 ) ); 1242 1243 $q = new WP_Query( array( 1244 'taxonomy' => 'bar', 1245 'tax_query' => array( 1246 array( 1247 'taxonomy' => 'foo', 1248 'terms' => array( $t ), 1249 ), 1250 ), 1251 ) ); 1422 $t = self::factory()->term->create( 1423 array( 1424 'taxonomy' => 'foo', 1425 ) 1426 ); 1427 1428 $q = new WP_Query( 1429 array( 1430 'taxonomy' => 'bar', 1431 'tax_query' => array( 1432 array( 1433 'taxonomy' => 'foo', 1434 'terms' => array( $t ), 1435 ), 1436 ), 1437 ) 1438 ); 1252 1439 1253 1440 $this->assertSame( 'bar', $q->get( 'taxonomy' ) ); … … 1259 1446 public function test_populate_term_query_var_from_tax_query() { 1260 1447 register_taxonomy( 'foo', 'post' ); 1261 $t = self::factory()->term->create( array( 1262 'taxonomy' => 'foo', 1263 'slug' => 'bar', 1264 ) ); 1265 1266 $q = new WP_Query( array( 1267 'tax_query' => array( 1268 array( 1269 'taxonomy' => 'foo', 1270 'terms' => array( 'bar' ), 1271 'field' => 'slug', 1272 ), 1273 ), 1274 ) ); 1448 $t = self::factory()->term->create( 1449 array( 1450 'taxonomy' => 'foo', 1451 'slug' => 'bar', 1452 ) 1453 ); 1454 1455 $q = new WP_Query( 1456 array( 1457 'tax_query' => array( 1458 array( 1459 'taxonomy' => 'foo', 1460 'terms' => array( 'bar' ), 1461 'field' => 'slug', 1462 ), 1463 ), 1464 ) 1465 ); 1275 1466 1276 1467 $this->assertSame( 'bar', $q->get( 'term' ) ); … … 1281 1472 public function test_populate_term_id_query_var_from_tax_query() { 1282 1473 register_taxonomy( 'foo', 'post' ); 1283 $t = self::factory()->term->create( array( 1284 'taxonomy' => 'foo', 1285 'slug' => 'bar', 1286 ) ); 1287 1288 $q = new WP_Query( array( 1289 'tax_query' => array( 1290 array( 1291 'taxonomy' => 'foo', 1292 'terms' => array( $t ), 1293 'field' => 'term_id', 1294 ), 1295 ), 1296 ) ); 1474 $t = self::factory()->term->create( 1475 array( 1476 'taxonomy' => 'foo', 1477 'slug' => 'bar', 1478 ) 1479 ); 1480 1481 $q = new WP_Query( 1482 array( 1483 'tax_query' => array( 1484 array( 1485 'taxonomy' => 'foo', 1486 'terms' => array( $t ), 1487 'field' => 'term_id', 1488 ), 1489 ), 1490 ) 1491 ); 1297 1492 1298 1493 $this->assertEquals( $t, $q->get( 'term_id' ) ); … … 1306 1501 public function test_populate_cat_category_name_query_var_from_tax_query() { 1307 1502 register_taxonomy( 'foo', 'post' ); 1308 $t = self::factory()->term->create( array( 1309 'taxonomy' => 'foo', 1310 ) ); 1311 $c = self::factory()->term->create( array( 1312 'taxonomy' => 'category', 1313 'slug' => 'bar', 1314 ) ); 1315 1316 $q = new WP_Query( array( 1317 'tax_query' => array( 1318 // Non-category should be skipped 1319 array( 1320 'taxonomy' => 'foo', 1321 'terms' => array( $t ), 1322 ), 1323 1324 // Empty terms mean that this one should be skipped 1325 array( 1326 'taxonomy' => 'category', 1327 'terms' => array(), 1328 ), 1329 1330 // Category and post tags should be skipped 1331 array( 1332 'taxonomy' => 'category', 1333 'terms' => array( $c ), 1334 ), 1335 ), 1336 ) ); 1503 $t = self::factory()->term->create( 1504 array( 1505 'taxonomy' => 'foo', 1506 ) 1507 ); 1508 $c = self::factory()->term->create( 1509 array( 1510 'taxonomy' => 'category', 1511 'slug' => 'bar', 1512 ) 1513 ); 1514 1515 $q = new WP_Query( 1516 array( 1517 'tax_query' => array( 1518 // Non-category should be skipped 1519 array( 1520 'taxonomy' => 'foo', 1521 'terms' => array( $t ), 1522 ), 1523 1524 // Empty terms mean that this one should be skipped 1525 array( 1526 'taxonomy' => 'category', 1527 'terms' => array(), 1528 ), 1529 1530 // Category and post tags should be skipped 1531 array( 1532 'taxonomy' => 'category', 1533 'terms' => array( $c ), 1534 ), 1535 ), 1536 ) 1537 ); 1337 1538 1338 1539 $this->assertEquals( $c, $q->get( 'cat' ) ); … … 1347 1548 public function test_populate_tag_id_query_var_from_tax_query() { 1348 1549 register_taxonomy( 'foo', 'post' ); 1349 $t = self::factory()->term->create( array( 1350 'taxonomy' => 'foo', 1351 ) ); 1352 $tag = self::factory()->term->create( array( 1353 'taxonomy' => 'post_tag', 1354 'slug' => 'bar', 1355 ) ); 1356 1357 $q = new WP_Query( array( 1358 'tax_query' => array( 1359 // Non-tag should be skipped 1360 array( 1361 'taxonomy' => 'foo', 1362 'terms' => array( $t ), 1363 ), 1364 1365 // Empty terms mean that this one should be skipped 1366 array( 1367 'taxonomy' => 'post_tag', 1368 'terms' => array(), 1369 ), 1370 1371 // Category and post tags should be skipped 1372 array( 1373 'taxonomy' => 'post_tag', 1374 'terms' => array( $tag ), 1375 ), 1376 ), 1377 ) ); 1550 $t = self::factory()->term->create( 1551 array( 1552 'taxonomy' => 'foo', 1553 ) 1554 ); 1555 $tag = self::factory()->term->create( 1556 array( 1557 'taxonomy' => 'post_tag', 1558 'slug' => 'bar', 1559 ) 1560 ); 1561 1562 $q = new WP_Query( 1563 array( 1564 'tax_query' => array( 1565 // Non-tag should be skipped 1566 array( 1567 'taxonomy' => 'foo', 1568 'terms' => array( $t ), 1569 ), 1570 1571 // Empty terms mean that this one should be skipped 1572 array( 1573 'taxonomy' => 'post_tag', 1574 'terms' => array(), 1575 ), 1576 1577 // Category and post tags should be skipped 1578 array( 1579 'taxonomy' => 'post_tag', 1580 'terms' => array( $tag ), 1581 ), 1582 ), 1583 ) 1584 ); 1378 1585 1379 1586 $this->assertEquals( $tag, $q->get( 'tag_id' ) ); … … 1389 1596 1390 1597 register_taxonomy( 'wptests_tax', 'post' ); 1391 $t = self::factory()->term->create( array( 1392 'taxonomy' => 'wptests_tax', 1393 'name' => $name, 1394 ) ); 1598 $t = self::factory()->term->create( 1599 array( 1600 'taxonomy' => 'wptests_tax', 1601 'name' => $name, 1602 ) 1603 ); 1395 1604 1396 1605 $p = self::factory()->post->create(); 1397 1606 wp_set_object_terms( $p, array( $t ), 'wptests_tax' ); 1398 1607 1399 $q = new WP_Query( array( 1400 'fields' => 'ids', 1401 'tax_query' => array( 1402 array( 1403 'taxonomy' => 'wptests_tax', 1404 'field' => 'name', 1405 'terms' => $name, 1406 ), 1407 ), 1408 ) ); 1608 $q = new WP_Query( 1609 array( 1610 'fields' => 'ids', 1611 'tax_query' => array( 1612 array( 1613 'taxonomy' => 'wptests_tax', 1614 'field' => 'name', 1615 'terms' => $name, 1616 ), 1617 ), 1618 ) 1619 ); 1409 1620 1410 1621 $this->assertEqualSets( array( $p ), $q->posts );
Note: See TracChangeset
for help on using the changeset viewer.