| 1480 | * @ticket 29031 |
| 1481 | * @group taxonomy |
| 1482 | */ |
| 1483 | public function test_tax_query_field_id_terms_order() { |
| 1484 | register_taxonomy( 'movie_genre', 'post' ); |
| 1485 | register_taxonomy( 'actors', 'post' ); |
| 1486 | $posts = $this->factory->post->create_many( 4 ); |
| 1487 | |
| 1488 | $mg_term = $this->factory->term->create( array( |
| 1489 | 'taxonomy' => 'movie_genre', |
| 1490 | 'slug' => 'action', |
| 1491 | ) ); |
| 1492 | |
| 1493 | $a_term_1 = $this->factory->term->create( array( |
| 1494 | 'taxonomy' => 'actors', |
| 1495 | ) ); |
| 1496 | |
| 1497 | $a_term_2 = $this->factory->term->create( array( |
| 1498 | 'taxonomy' => 'actors', |
| 1499 | ) ); |
| 1500 | |
| 1501 | // a_term_1 does not have any posts. |
| 1502 | wp_set_object_terms( $posts[0], $mg_term, 'movie_genre' ); |
| 1503 | wp_set_object_terms( $posts[0], $a_term_2, 'actors' ); |
| 1504 | |
| 1505 | wp_set_object_terms( $posts[1], $mg_term, 'movie_genre' ); |
| 1506 | wp_set_object_terms( $posts[2], $a_term_2, 'actors' ); |
| 1507 | |
| 1508 | $query = new WP_Query( array( |
| 1509 | 'fields' => 'ids', |
| 1510 | 'update_post_term_cache' => false, |
| 1511 | 'update_post_meta_cache' => false, |
| 1512 | 'relation' => 'AND', |
| 1513 | 'tax_query' => array( |
| 1514 | array( |
| 1515 | 'taxonomy' => 'movie_genre', |
| 1516 | 'field' => 'slug', |
| 1517 | 'terms' => array( 'action' ), |
| 1518 | ), |
| 1519 | array( |
| 1520 | 'taxonomy' => 'actors', |
| 1521 | 'field' => 'id', |
| 1522 | 'terms' => array( $a_term_1, $a_term_2 ), |
| 1523 | 'operator' => 'IN', |
| 1524 | ) |
| 1525 | ), |
| 1526 | ) ); |
| 1527 | |
| 1528 | $found = $query->get_posts(); |
| 1529 | $this->assertEqualSets( array( $posts[0] ), $found ); |
| 1530 | |
| 1531 | // Try again, with the 'actors' terms reversed in order. |
| 1532 | $query = new WP_Query( array( |
| 1533 | 'fields' => 'ids', |
| 1534 | 'update_post_term_cache' => false, |
| 1535 | 'update_post_meta_cache' => false, |
| 1536 | 'relation' => 'AND', |
| 1537 | 'tax_query' => array( |
| 1538 | array( |
| 1539 | 'taxonomy' => 'movie_genre', |
| 1540 | 'field' => 'slug', |
| 1541 | 'terms' => array( 'action' ), |
| 1542 | ), |
| 1543 | array( |
| 1544 | 'taxonomy' => 'actors', |
| 1545 | 'field' => 'id', |
| 1546 | 'terms' => array( $a_term_2, $a_term_1 ), |
| 1547 | 'operator' => 'IN', |
| 1548 | ) |
| 1549 | ), |
| 1550 | ) ); |
| 1551 | |
| 1552 | $found = $query->get_posts(); |
| 1553 | $this->assertEqualSets( array( $posts[0] ), $found ); |
| 1554 | } |
| 1555 | |
| 1556 | /** |