From a57a612c9584c79851e4b0ef07df958af665a8c9 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 27 Jan 2015 20:03:50 +0000 Subject: [PATCH] Improve organiation of tax_query and meta_query unit tests. `meta_query` tests have been moved to `tests/phpunit/tests/query/metaQuery.php` and `tax_query` tests to `tests/phpunit/tests/query/taxQuery.php`. This is an improvement because (a) it better corresponds to the way that other `WP_Query` parameter tests are organized, (b) splitting meta and tax tests into separate classes simplifies the required `@group` annotations, and (c) the tests have nothing to do with posts per se, and so do not belong in the `post` subdirectory. The tests previously found at `tests/phpunit/tests/query/taxQuery.php` have been moved to `isTerm.php` in the same directory. These tests are related to the `is_*` functions that have to do with taxonomy terms, like `is_category()`. See #26999. git-svn-id: https://develop.svn.wordpress.org/trunk@31286 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/post/query.php | 2978 ----------------------- tests/phpunit/tests/query/isTerm.php | 283 +++ tests/phpunit/tests/query/metaQuery.php | 1561 ++++++++++++ tests/phpunit/tests/query/taxQuery.php | 1528 ++++++++++-- 4 files changed, 3121 insertions(+), 3229 deletions(-) create mode 100644 tests/phpunit/tests/query/isTerm.php create mode 100644 tests/phpunit/tests/query/metaQuery.php diff --git a/tests/phpunit/tests/post/query.php b/tests/phpunit/tests/post/query.php index 722cad77ef..4d251b5f8b 100644 --- a/tests/phpunit/tests/post/query.php +++ b/tests/phpunit/tests/post/query.php @@ -5,2632 +5,6 @@ class Tests_Post_Query extends WP_UnitTestCase { parent::setUp(); } - /** - * @group meta - */ - public function test_meta_query_no_key() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - add_post_meta( $p2, 'oof', 'bar' ); - add_post_meta( $p3, 'oof', 'baz' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'value' => 'bar', - ), - ), - ) ); - - $expected = array( $p1, $p2 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_no_value() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - add_post_meta( $p2, 'oof', 'bar' ); - add_post_meta( $p3, 'oof', 'baz' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'oof', - ), - ), - ) ); - - $expected = array( $p2, $p3 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_default() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'bar', - ), - ), - ) ); - - $expected = array( $p1 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_equals() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'bar', - 'compare' => '=', - ), - ), - ) ); - - $expected = array( $p1 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_not_equals() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - add_post_meta( $p2, 'foo', 'baz' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'bar', - 'compare' => '!=', - ), - ), - ) ); - - $expected = array( $p2 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_arithmetic_comparisons() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', '1' ); - add_post_meta( $p2, 'foo', '2' ); - add_post_meta( $p3, 'foo', '3' ); - - // < - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 2, - 'compare' => '<', - ), - ), - ) ); - - $expected = array( $p1 ); - $this->assertEqualSets( $expected, $query->posts ); - - // <= - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 2, - 'compare' => '<=', - ), - ), - ) ); - - $expected = array( $p1, $p2 ); - $this->assertEqualSets( $expected, $query->posts ); - - // >= - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 2, - 'compare' => '>=', - ), - ), - ) ); - - $expected = array( $p2, $p3 ); - $this->assertEqualSets( $expected, $query->posts ); - - // > - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 2, - 'compare' => '>', - ), - ), - ) ); - - $expected = array( $p3 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_like() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'ba', - 'compare' => 'LIKE', - ), - ), - ) ); - - $expected = array( $p1 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_not_like() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - add_post_meta( $p2, 'foo', 'rab' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'ba', - 'compare' => 'NOT LIKE', - ), - ), - ) ); - - $expected = array( $p2 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_between_not_between() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', '1' ); - add_post_meta( $p2, 'foo', '10' ); - add_post_meta( $p3, 'foo', '100' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => array( 9, 12 ), - 'compare' => 'BETWEEN', - 'type' => 'NUMERIC', - ), - ), - ) ); - - $expected = array( $p2 ); - $this->assertEqualSets( $expected, $query->posts ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => array( 9, 12 ), - 'compare' => 'NOT BETWEEN', - 'type' => 'NUMERIC', - ), - ), - ) ); - - $expected = array( $p1, $p3 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_regexp_rlike() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - add_post_meta( $p2, 'foo', 'baz' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'z$', - 'compare' => 'REGEXP', - ), - ), - ) ); - - $expected = array( $p2 ); - $this->assertEqualSets( $expected, $query->posts ); - - // RLIKE is a synonym for REGEXP. - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'z$', - 'compare' => 'RLIKE', - ), - ), - ) ); - - $expected = array( $p2 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_single_query_compare_not_regexp() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - add_post_meta( $p2, 'foo', 'baz' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'z$', - 'compare' => 'NOT REGEXP', - ), - ), - ) ); - - $expected = array( $p1 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_relation_default() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'foo value 1' ); - add_post_meta( $p1, 'bar', 'bar value 1' ); - add_post_meta( $p2, 'foo', 'foo value 1' ); - add_post_meta( $p2, 'bar', 'bar value 2' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo', - 'value' => 'foo value 1', - ), - array( - 'key' => 'bar', - 'value' => 'bar value 1', - ), - ), - ) ); - - $expected = array( $p1 ); - $this->assertEquals( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_relation_or() { - $post_id = $this->factory->post->create(); - add_post_meta( $post_id, 'foo', rand_str() ); - add_post_meta( $post_id, 'foo', rand_str() ); - $post_id2 = $this->factory->post->create(); - add_post_meta( $post_id2, 'bar', 'val2' ); - $post_id3 = $this->factory->post->create(); - add_post_meta( $post_id3, 'baz', rand_str() ); - $post_id4 = $this->factory->post->create(); - add_post_meta( $post_id4, 'froo', rand_str() ); - $post_id5 = $this->factory->post->create(); - add_post_meta( $post_id5, 'tango', 'val2' ); - $post_id6 = $this->factory->post->create(); - add_post_meta( $post_id6, 'bar', 'val1' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - array( - 'key' => 'foo' - ), - array( - 'key' => 'bar', - 'value' => 'val2' - ), - array( - 'key' => 'baz' - ), - array( - 'key' => 'froo' - ), - 'relation' => 'OR', - ), - ) ); - - $expected = array( $post_id, $post_id2, $post_id3, $post_id4 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - public function test_meta_query_relation_and() { - $post_id = $this->factory->post->create(); - add_post_meta( $post_id, 'foo', rand_str() ); - add_post_meta( $post_id, 'foo', rand_str() ); - $post_id2 = $this->factory->post->create(); - add_post_meta( $post_id2, 'bar', 'val2' ); - add_post_meta( $post_id2, 'foo', rand_str() ); - $post_id3 = $this->factory->post->create(); - add_post_meta( $post_id3, 'baz', rand_str() ); - $post_id4 = $this->factory->post->create(); - add_post_meta( $post_id4, 'froo', rand_str() ); - $post_id5 = $this->factory->post->create(); - add_post_meta( $post_id5, 'tango', 'val2' ); - $post_id6 = $this->factory->post->create(); - add_post_meta( $post_id6, 'bar', 'val1' ); - add_post_meta( $post_id6, 'foo', rand_str() ); - $post_id7 = $this->factory->post->create(); - add_post_meta( $post_id7, 'foo', rand_str() ); - add_post_meta( $post_id7, 'froo', rand_str() ); - add_post_meta( $post_id7, 'baz', rand_str() ); - add_post_meta( $post_id7, 'bar', 'val2' ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'foo' - ), - array( - 'key' => 'bar', - 'value' => 'val2' - ), - array( - 'key' => 'baz' - ), - array( - 'key' => 'froo' - ), - 'relation' => 'AND', - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $post_id7 ); - $this->assertEqualSets( $expected, $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'foo' - ), - array( - 'key' => 'bar', - ), - 'relation' => 'AND', - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $post_id2, $post_id6, $post_id7 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 30681 - */ - public function test_meta_query_compare_exists() { - $posts = $this->factory->post->create_many( 3 ); - add_post_meta( $posts[0], 'foo', 'bar' ); - add_post_meta( $posts[2], 'foo', 'baz' ); - - $query = new WP_Query( array( - 'fields' => 'ids', - 'meta_query' => array( - array( - 'compare' => 'EXISTS', - 'key' => 'foo', - ), - ), - ) ); - - $this->assertEqualSets( array( $posts[0], $posts[2] ), $query->posts ); - } - - /** - * @ticket 30681 - */ - public function test_meta_query_compare_exists_with_value_should_convert_to_equals() { - $posts = $this->factory->post->create_many( 3 ); - add_post_meta( $posts[0], 'foo', 'bar' ); - add_post_meta( $posts[2], 'foo', 'baz' ); - - $query = new WP_Query( array( - 'fields' => 'ids', - 'meta_query' => array( - array( - 'compare' => 'EXISTS', - 'value' => 'baz', - 'key' => 'foo', - ), - ), - ) ); - - $this->assertEqualSets( array( $posts[2] ), $query->posts ); - } - - /** - * @ticket 30681 - */ - public function test_meta_query_compare_not_exists_should_ignore_value() { - $posts = $this->factory->post->create_many( 3 ); - add_post_meta( $posts[0], 'foo', 'bar' ); - add_post_meta( $posts[2], 'foo', 'baz' ); - - $query = new WP_Query( array( - 'fields' => 'ids', - 'meta_query' => array( - array( - 'compare' => 'NOT EXISTS', - 'value' => 'bar', - 'key' => 'foo', - ), - ), - ) ); - - $this->assertEqualSets( array( $posts[1] ), $query->posts ); - } - - /** - * @ticket 18158 - * @group meta - */ - public function test_meta_query_compare_not_exists() { - $post_id = $this->factory->post->create(); - add_post_meta( $post_id, 'foo', rand_str() ); - $post_id2 = $this->factory->post->create(); - add_post_meta( $post_id2, 'bar', rand_str() ); - $post_id3 = $this->factory->post->create(); - add_post_meta( $post_id3, 'bar', rand_str() ); - $post_id4 = $this->factory->post->create(); - add_post_meta( $post_id4, 'baz', rand_str() ); - $post_id5 = $this->factory->post->create(); - add_post_meta( $post_id5, 'foo', rand_str() ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'foo', - 'compare' => 'NOT EXISTS', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $post_id2, $post_id3, $post_id4 ); - $this->assertEqualSets( $expected, $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'foo', - 'compare' => 'NOT EXISTS', - ), - array( - 'key' => 'bar', - 'compare' => 'NOT EXISTS', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $post_id4 ); - $this->assertEquals( $expected, $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'foo', - 'compare' => 'NOT EXISTS', - ), - array( - 'key' => 'bar', - 'compare' => 'NOT EXISTS', - ), - array( - 'key' => 'baz', - 'compare' => 'NOT EXISTS', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $this->assertEquals( 0, count( $query->posts ) ); - } - - /** - * @ticket 29062 - */ - public function test_meta_query_compare_not_exists_with_another_condition_relation_or() { - $posts = $this->factory->post->create_many( 4 ); - update_post_meta( $posts[0], 'color', 'orange' ); - update_post_meta( $posts[1], 'color', 'blue' ); - update_post_meta( $posts[1], 'vegetable', 'onion' ); - update_post_meta( $posts[2], 'vegetable', 'shallot' ); - - $post_3_meta = get_post_meta( $posts[3] ); - foreach ( $post_3_meta as $meta_key => $meta_value ) { - delete_post_meta( $posts[3], $meta_key ); - } - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'vegetable', - 'value' => 'onion', - ), - array( - 'key' => 'color', - 'compare' => 'NOT EXISTS', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[1], $posts[2], $posts[3] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_or_compare_equals() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'vegetable', - 'value' => 'onion', - 'compare' => '=', - ), - array( - 'key' => 'vegetable', - 'value' => 'shallot', - 'compare' => '=', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[1], $posts[2] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_or_compare_equals_different_keys() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'vegetable', - 'value' => 'onion', - 'compare' => '=', - ), - array( - 'key' => 'color', - 'value' => 'orange', - 'compare' => '=', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[0], $posts[1] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_or_compare_equals_and_in() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'vegetable', - 'value' => 'onion', - 'compare' => '=', - ), - array( - 'key' => 'color', - 'value' => array( 'orange', 'green' ), - 'compare' => 'IN', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[0], $posts[1] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_or_compare_equals_and_like() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'vegetable', - 'value' => 'onion', - 'compare' => '=', - ), - array( - 'key' => 'vegetable', - 'value' => 'hall', - 'compare' => 'LIKE', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[1], $posts[2] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_or_compare_equals_and_between() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'number_of_colors', '2' ); - add_post_meta( $posts[1], 'number_of_colors', '5' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'vegetable', - 'value' => 'shallot', - 'compare' => '=', - ), - array( - 'key' => 'number_of_colors', - 'value' => array( 1, 3 ), - 'compare' => 'BETWEEN', - 'type' => 'SIGNED', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[0], $posts[2] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_and_compare_in_same_keys() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - add_post_meta( $posts[3], 'vegetable', 'banana' ); - add_post_meta( $posts[3], 'vegetable', 'onion' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'vegetable', - 'value' => array( 'onion', 'shallot' ), - 'compare' => 'IN', - ), - array( - 'key' => 'vegetable', - 'value' => array( 'banana' ), - 'compare' => 'IN', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[3] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_and_compare_in_different_keys() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[1], 'vegetable', 'shallot' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - add_post_meta( $posts[3], 'vegetable', 'banana' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'vegetable', - 'value' => array( 'onion', 'shallot' ), - 'compare' => 'IN', - ), - array( - 'key' => 'color', - 'value' => array( 'blue' ), - 'compare' => 'IN', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[1] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_and_compare_not_equals() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - add_post_meta( $posts[3], 'vegetable', 'banana' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'vegetable', - 'value' => 'onion', - 'compare' => '!=', - ), - array( - 'key' => 'vegetable', - 'value' => 'shallot', - 'compare' => '!=', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[3] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_and_compare_not_equals_different_keys() { - $posts = $this->factory->post->create_many( 4 ); - - // !shallot, but orange. - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[0], 'vegetable', 'onion' ); - - // !orange, but shallot. - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'shallot' ); - - // Neither. - add_post_meta( $posts[2], 'color', 'blue' ); - add_post_meta( $posts[2], 'vegetable', 'onion' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'vegetable', - 'value' => 'shallot', - 'compare' => '!=', - ), - array( - 'key' => 'color', - 'value' => 'orange', - 'compare' => '!=', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[2] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_and_compare_not_equals_not_in() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - add_post_meta( $posts[3], 'vegetable', 'banana' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'vegetable', - 'value' => 'onion', - 'compare' => '!=', - ), - array( - 'key' => 'vegetable', - 'value' => array( 'shallot' ), - 'compare' => 'NOT IN', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[3] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 24093 - */ - public function test_meta_query_relation_and_compare_not_equals_and_not_like() { - $posts = $this->factory->post->create_many( 4 ); - add_post_meta( $posts[0], 'color', 'orange' ); - add_post_meta( $posts[1], 'color', 'blue' ); - add_post_meta( $posts[1], 'vegetable', 'onion' ); - add_post_meta( $posts[2], 'vegetable', 'shallot' ); - add_post_meta( $posts[3], 'vegetable', 'banana' ); - - $query = new WP_Query( array( - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'vegetable', - 'value' => 'onion', - 'compare' => '!=', - ), - array( - 'key' => 'vegetable', - 'value' => 'hall', - 'compare' => 'NOT LIKE', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $expected = array( $posts[3] ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 23033 - * @group meta - */ - public function test_meta_query_decimal_results() { - $post_1 = $this->factory->post->create(); - $post_2 = $this->factory->post->create(); - $post_3 = $this->factory->post->create(); - $post_4 = $this->factory->post->create(); - - update_post_meta( $post_1, 'decimal_value', '-0.3' ); - update_post_meta( $post_2, 'decimal_value', '0.23409844' ); - update_post_meta( $post_3, 'decimal_value', '0.3' ); - update_post_meta( $post_4, 'decimal_value', '0.4' ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => '.300', - 'compare' => '=', - 'type' => 'DECIMAL(10,2)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_3 ), $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => '0.35', - 'compare' => '>', - 'type' => 'DECIMAL(10,2)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_4 ), $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => '0.3', - 'compare' => '>=', - 'type' => 'DECIMAL(10,2)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_3, $post_4 ), $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => '0', - 'compare' => '<', - 'type' => 'DECIMAL(10,2)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_1 ), $query->posts, 'ID' ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => '0.3', - 'compare' => '<=', - 'type' => 'DECIMAL(10,2)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_1, $post_2, $post_3 ), $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => array( 0.23409845, .31 ), - 'compare' => 'BETWEEN', - 'type' => 'DECIMAL(10, 10)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_3 ), $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => array( 0.23409845, .31 ), - 'compare' => 'NOT BETWEEN', - 'type' => 'DECIMAL(10,10)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_1, $post_2, $post_4 ), $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => '.3', - 'compare' => 'LIKE', - 'type' => 'DECIMAL(10,2)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_1, $post_3 ), $query->posts ); - - $query = new WP_Query( array( - 'meta_query' => array( - array( - 'key' => 'decimal_value', - 'value' => '.3', - 'compare' => 'NOT LIKE', - 'type' => 'DECIMAL(10,2)' - ) - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_2, $post_4 ), $query->posts ); - - $query = new WP_Query( array( - 'orderby' => 'meta_value', - 'order' => 'DESC', - 'meta_key' => 'decimal_value', - 'meta_type' => 'DECIMAL(10, 2)', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - $this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), $query->posts ); - } - - /** - * @group meta - * @ticket 29604 - */ - public function test_meta_query_with_orderby_meta_value_relation_or() { - $posts = $this->factory->post->create_many( 4 ); - update_post_meta( $posts[0], 'foo', 5 ); - update_post_meta( $posts[1], 'foo', 6 ); - update_post_meta( $posts[2], 'foo', 4 ); - update_post_meta( $posts[3], 'foo', 7 ); - - update_post_meta( $posts[0], 'bar1', 'baz' ); - update_post_meta( $posts[1], 'bar1', 'baz' ); - update_post_meta( $posts[2], 'bar2', 'baz' ); - - $query = new WP_Query( array( - 'orderby' => 'meta_value', - 'order' => 'ASC', - 'meta_key' => 'foo', - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'bar1', - 'value' => 'baz', - 'compare' => '=', - ), - array( - 'key' => 'bar2', - 'value' => 'baz', - 'compare' => '=', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $query->posts ); - } - - /** - * @group meta - * @ticket 29604 - */ - public function test_meta_query_with_orderby_meta_value_relation_and() { - $posts = $this->factory->post->create_many( 4 ); - update_post_meta( $posts[0], 'foo', 5 ); - update_post_meta( $posts[1], 'foo', 6 ); - update_post_meta( $posts[2], 'foo', 4 ); - update_post_meta( $posts[3], 'foo', 7 ); - - update_post_meta( $posts[0], 'bar1', 'baz' ); - update_post_meta( $posts[1], 'bar1', 'baz' ); - update_post_meta( $posts[2], 'bar1', 'baz' ); - update_post_meta( $posts[3], 'bar1', 'baz' ); - update_post_meta( $posts[0], 'bar2', 'baz' ); - update_post_meta( $posts[1], 'bar2', 'baz' ); - update_post_meta( $posts[2], 'bar2', 'baz' ); - - $query = new WP_Query( array( - 'orderby' => 'meta_value', - 'order' => 'ASC', - 'meta_key' => 'foo', - 'meta_query' => array( - 'relation' => 'AND', - array( - 'key' => 'bar1', - 'value' => 'baz', - 'compare' => '=', - ), - array( - 'key' => 'bar2', - 'value' => 'baz', - 'compare' => '=', - ), - ), - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'fields' => 'ids', - ) ); - - $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $query->posts ); - } - - /** - * @ticket 29642 - * @group meta - */ - public function test_meta_query_nested() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - add_post_meta( $p2, 'foo2', 'bar' ); - add_post_meta( $p3, 'foo2', 'bar' ); - add_post_meta( $p3, 'foo3', 'bar' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_term_meta_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'foo', - 'value' => 'bar', - ), - array( - 'relation' => 'AND', - array( - 'key' => 'foo2', - 'value' => 'bar', - ), - array( - 'key' => 'foo3', - 'value' => 'bar', - ), - ), - ), - ) ); - - $expected = array( $p1, $p3 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @ticket 29642 - * @group meta - */ - public function test_meta_query_nested_two_levels_deep() { - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - add_post_meta( $p1, 'foo', 'bar' ); - add_post_meta( $p3, 'foo2', 'bar' ); - add_post_meta( $p3, 'foo3', 'bar' ); - add_post_meta( $p3, 'foo4', 'bar' ); - - $query = new WP_Query( array( - 'update_post_meta_cache' => false, - 'update_term_meta_cache' => false, - 'fields' => 'ids', - 'meta_query' => array( - 'relation' => 'OR', - array( - 'key' => 'foo', - 'value' => 'bar', - ), - array( - 'relation' => 'OR', - array( - 'key' => 'foo2', - 'value' => 'bar', - ), - array( - 'relation' => 'AND', - array( - 'key' => 'foo3', - 'value' => 'bar', - ), - array( - 'key' => 'foo4', - 'value' => 'bar', - ), - ), - ), - ), - ) ); - - $expected = array( $p1, $p3 ); - $this->assertEqualSets( $expected, $query->posts ); - } - - /** - * @group meta - */ - function test_meta_between_not_between() { - $post_id = $this->factory->post->create(); - add_post_meta( $post_id, 'time', 500 ); - $post_id2 = $this->factory->post->create(); - add_post_meta( $post_id2, 'time', 1001 ); - $post_id3 = $this->factory->post->create(); - add_post_meta( $post_id3, 'time', 0 ); - $post_id4 = $this->factory->post->create(); - add_post_meta( $post_id4, 'time', 1 ); - $post_id5 = $this->factory->post->create(); - add_post_meta( $post_id5, 'time', 1000 ); - - $args = array( - 'meta_key' => 'time', - 'meta_value' => array( 1, 1000 ), - 'meta_type' => 'numeric', - 'meta_compare' => 'NOT BETWEEN' - ); - - $query = new WP_Query( $args ); - $this->assertEquals( 2, count ( $query->posts ) ); - foreach ( $query->posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $posts = wp_list_pluck( $query->posts, 'ID' ); - $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts ); - - $args = array( - 'meta_key' => 'time', - 'meta_value' => array( 1, 1000 ), - 'meta_type' => 'numeric', - 'meta_compare' => 'BETWEEN' - ); - - $query = new WP_Query( $args ); - $this->assertEquals( 3, count ( $query->posts ) ); - foreach ( $query->posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $posts = wp_list_pluck( $query->posts, 'ID' ); - $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts ); - } - - /** - * @ticket 16829 - * @group meta - */ - function test_meta_default_compare() { - // compare should default to IN when meta_value is an array - $post_id = $this->factory->post->create(); - add_post_meta( $post_id, 'foo', 'bar' ); - $post_id2 = $this->factory->post->create(); - add_post_meta( $post_id2, 'bar', 'baz' ); - $post_id3 = $this->factory->post->create(); - add_post_meta( $post_id3, 'foo', 'baz' ); - $post_id4 = $this->factory->post->create(); - add_post_meta( $post_id4, 'baz', 'bar' ); - $post_id5 = $this->factory->post->create(); - add_post_meta( $post_id5, 'foo', rand_str() ); - - $posts = get_posts( array( - 'meta_key' => 'foo', - 'meta_value' => array( 'bar', 'baz' ) - ) ); - - $this->assertEquals( 2, count( $posts ) ); - $posts = wp_list_pluck( $posts, 'ID' ); - $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); - - $posts = get_posts( array( - 'meta_key' => 'foo', - 'meta_value' => array( 'bar', 'baz' ), - 'meta_compare' => 'IN' - ) ); - - $this->assertEquals( 2, count( $posts ) ); - foreach ( $posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $posts = wp_list_pluck( $posts, 'ID' ); - $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); - } - - /** - * @ticket 17264 - * @group meta - */ - function test_duplicate_posts_when_no_key() { - $post_id = $this->factory->post->create(); - add_post_meta( $post_id, 'city', 'Lorem' ); - add_post_meta( $post_id, 'address', '123 Lorem St.' ); - $post_id2 = $this->factory->post->create(); - add_post_meta( $post_id2, 'city', 'Lorem' ); - $post_id3 = $this->factory->post->create(); - add_post_meta( $post_id3, 'city', 'Loren' ); - - $args = array( - 'meta_query' => array( - array( - 'value' => 'lorem', - 'compare' => 'LIKE' - ) - ) - ); - - $posts = get_posts( $args ); - $this->assertEquals( 2, count( $posts ) ); - foreach ( $posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $posts = wp_list_pluck( $posts, 'ID' ); - $this->assertEqualSets( array( $post_id, $post_id2 ), $posts ); - } - - /** - * @ticket 15292 - * @group meta - */ - function test_empty_meta_value() { - $post_id = $this->factory->post->create(); - add_post_meta( $post_id, 'foo', '0' ); - add_post_meta( $post_id, 'bar', 0 ); - $post_id2 = $this->factory->post->create(); - add_post_meta( $post_id2, 'foo', 1 ); - $post_id3 = $this->factory->post->create(); - add_post_meta( $post_id3, 'baz', 0 ); - $post_id4 = $this->factory->post->create(); - add_post_meta( $post_id4, 'baz', 0 ); - $post_id5 = $this->factory->post->create(); - add_post_meta( $post_id5, 'baz', 0 ); - add_post_meta( $post_id5, 'bar', '0' ); - $post_id6 = $this->factory->post->create(); - add_post_meta( $post_id6, 'baz', 0 ); - - $q = new WP_Query( array( 'meta_key' => 'foo', 'meta_value' => '0' ) ); - $this->assertEquals( 1, count ( $q->posts ) ); - foreach ( $q->posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $this->assertEquals( $post_id, $q->posts[0]->ID ); - - $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) ); - $this->assertEquals( 2, count ( $posts ) ); - foreach ( $posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $posts = wp_list_pluck( $posts, 'ID' ); - $this->assertEqualSets( array( $post_id, $post_id5 ), $posts ); - - $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => 0 ) ); - $this->assertEquals( 2, count ( $posts ) ); - foreach ( $posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $posts = wp_list_pluck( $posts, 'ID' ); - $this->assertEqualSets( array( $post_id, $post_id5 ), $posts ); - - $posts = get_posts( array( 'meta_value' => 0 ) ); - $this->assertEquals( 5, count ( $posts ) ); - foreach ( $posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $posts = wp_list_pluck( $posts, 'ID' ); - $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts ); - - $posts = get_posts( array( 'meta_value' => '0' ) ); - $this->assertEquals( 5, count ( $posts ) ); - foreach ( $posts as $post ) { - $this->assertInstanceOf( 'WP_Post', $post ); - $this->assertEquals( 'raw', $post->filter ); - } - $posts = wp_list_pluck( $posts, 'ID' ); - $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_single_term_field_slug() { - $t = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo' ), - 'field' => 'slug', - ), - ), - ) ); - - $this->assertEquals( array( $p1 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_single_term_field_name() { - $t = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( 'Foo' ), - 'field' => 'name', - ), - ), - ) ); - - $this->assertEquals( array( $p1 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_single_term_field_term_taxonomy_id() { - $t = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - $tt_ids = wp_set_post_terms( $p1, $t, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => $tt_ids, - 'field' => 'term_taxonomy_id', - ), - ), - ) ); - - $this->assertEquals( array( $p1 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_single_term_field_term_id() { - $t = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( $t ), - 'field' => 'term_id', - ), - ), - ) ); - - $this->assertEquals( array( $p1 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_single_term_operator_in() { - $t = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo' ), - 'field' => 'slug', - 'operator' => 'IN', - ), - ), - ) ); - - $this->assertEquals( array( $p1 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_single_term_operator_not_in() { - $t = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo' ), - 'field' => 'slug', - 'operator' => 'NOT IN', - ), - ), - ) ); - - $this->assertEquals( array( $p2 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_single_term_operator_and() { - $t = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo' ), - 'field' => 'slug', - 'operator' => 'AND', - ), - ), - ) ); - - $this->assertEquals( array( $p1 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_multiple_terms_operator_in() { - $t1 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $t2 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'bar', - 'name' => 'Bar', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t1, 'category' ); - wp_set_post_terms( $p2, $t2, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo', 'bar' ), - 'field' => 'slug', - 'operator' => 'IN', - ), - ), - ) ); - - $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_multiple_terms_operator_not_in() { - $t1 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $t2 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'bar', - 'name' => 'Bar', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t1, 'category' ); - wp_set_post_terms( $p2, $t2, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo', 'bar' ), - 'field' => 'slug', - 'operator' => 'NOT IN', - ), - ), - ) ); - - $this->assertEquals( array( $p3 ), $q->posts ); - } - - /** - * @group taxonomy - * @ticket 18105 - */ - public function test_tax_query_single_query_multiple_queries_operator_not_in() { - $t1 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $t2 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'bar', - 'name' => 'Bar', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_post_terms( $p1, $t1, 'category' ); - wp_set_post_terms( $p2, $t2, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo' ), - 'field' => 'slug', - 'operator' => 'NOT IN', - ), - array( - 'taxonomy' => 'category', - 'terms' => array( 'bar' ), - 'field' => 'slug', - 'operator' => 'NOT IN', - ), - ), - ) ); - - $this->assertEquals( array( $p3 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_single_query_multiple_terms_operator_and() { - $t1 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $t2 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'bar', - 'name' => 'Bar', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, $t1, 'category' ); - wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo', 'bar' ), - 'field' => 'slug', - 'operator' => 'AND', - ), - ), - ) ); - - $this->assertEquals( array( $p2 ), $q->posts ); - } - - /** - * @ticket 29181 - */ - public function test_tax_query_operator_not_exists() { - register_taxonomy( 'wptests_tax1', 'post' ); - register_taxonomy( 'wptests_tax2', 'post' ); - - $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); - $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); - - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); - wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'orderby' => 'ID', - 'order' => 'ASC', - 'tax_query' => array( - array( - 'taxonomy' => 'wptests_tax2', - 'operator' => 'NOT EXISTS', - ), - ), - ) ); - - $this->assertEqualSets( array( $p1, $p3 ), $q->posts ); - } - - /** - * @ticket 29181 - */ - public function test_tax_query_operator_exists() { - register_taxonomy( 'wptests_tax1', 'post' ); - register_taxonomy( 'wptests_tax2', 'post' ); - - $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); - $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); - - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); - wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'orderby' => 'ID', - 'order' => 'ASC', - 'tax_query' => array( - array( - 'taxonomy' => 'wptests_tax2', - 'operator' => 'EXISTS', - ), - ), - ) ); - - $this->assertEqualSets( array( $p2 ), $q->posts ); - } - - /** - * @ticket 29181 - */ - public function test_tax_query_operator_exists_should_ignore_terms() { - register_taxonomy( 'wptests_tax1', 'post' ); - register_taxonomy( 'wptests_tax2', 'post' ); - - $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); - $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); - - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); - wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'orderby' => 'ID', - 'order' => 'ASC', - 'tax_query' => array( - array( - 'taxonomy' => 'wptests_tax2', - 'operator' => 'EXISTS', - 'terms' => array( 'foo', 'bar' ), - ), - ), - ) ); - - $this->assertEqualSets( array( $p2 ), $q->posts ); - } - - /** - * @ticket 29181 - */ - public function test_tax_query_operator_exists_with_no_taxonomy() { - register_taxonomy( 'wptests_tax1', 'post' ); - register_taxonomy( 'wptests_tax2', 'post' ); - - $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); - $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); - - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); - wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'orderby' => 'ID', - 'order' => 'ASC', - 'tax_query' => array( - array( - 'operator' => 'EXISTS', - ), - ), - ) ); - - $this->assertEmpty( $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_multiple_queries_relation_and() { - $t1 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $t2 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'bar', - 'name' => 'Bar', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, $t1, 'category' ); - wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo' ), - 'field' => 'slug', - ), - array( - 'taxonomy' => 'category', - 'terms' => array( 'bar' ), - 'field' => 'slug', - ), - ), - ) ); - - $this->assertEquals( array( $p2 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_multiple_queries_relation_or() { - $t1 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $t2 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'bar', - 'name' => 'Bar', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, $t1, 'category' ); - wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - 'relation' => 'OR', - array( - 'taxonomy' => 'category', - 'terms' => array( 'foo' ), - 'field' => 'slug', - ), - array( - 'taxonomy' => 'category', - 'terms' => array( 'bar' ), - 'field' => 'slug', - ), - ), - ) ); - - $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_multiple_queries_different_taxonomies() { - $t1 = $this->factory->term->create( array( - 'taxonomy' => 'post_tag', - 'slug' => 'foo', - 'name' => 'Foo', - ) ); - $t2 = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'bar', - 'name' => 'Bar', - ) ); - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, $t1, 'post_tag' ); - wp_set_object_terms( $p2, $t2, 'category' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - 'relation' => 'OR', - array( - 'taxonomy' => 'post_tag', - 'terms' => array( 'foo' ), - 'field' => 'slug', - ), - array( - 'taxonomy' => 'category', - 'terms' => array( 'bar' ), - 'field' => 'slug', - ), - ), - ) ); - - $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); - } - - /** - * @ticket 29738 - * @group taxonomy - */ - public function test_tax_query_two_nested_queries() { - register_taxonomy( 'foo', 'post' ); - register_taxonomy( 'bar', 'post' ); - - $foo_term_1 = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $foo_term_2 = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $bar_term_1 = $this->factory->term->create( array( - 'taxonomy' => 'bar', - ) ); - $bar_term_2 = $this->factory->term->create( array( - 'taxonomy' => 'bar', - ) ); - - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); - wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); - wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); - wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); - wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); - wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - 'relation' => 'OR', - array( - 'relation' => 'AND', - array( - 'taxonomy' => 'foo', - 'terms' => array( $foo_term_1 ), - 'field' => 'term_id', - ), - array( - 'taxonomy' => 'bar', - 'terms' => array( $bar_term_1 ), - 'field' => 'term_id', - ), - ), - array( - 'relation' => 'AND', - array( - 'taxonomy' => 'foo', - 'terms' => array( $foo_term_2 ), - 'field' => 'term_id', - ), - array( - 'taxonomy' => 'bar', - 'terms' => array( $bar_term_2 ), - 'field' => 'term_id', - ), - ), - ), - ) ); - - _unregister_taxonomy( 'foo' ); - _unregister_taxonomy( 'bar' ); - - $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); - } - - /** - * @ticket 29738 - * @group taxonomy - */ - public function test_tax_query_one_nested_query_one_first_order_query() { - register_taxonomy( 'foo', 'post' ); - register_taxonomy( 'bar', 'post' ); - - $foo_term_1 = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $foo_term_2 = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $bar_term_1 = $this->factory->term->create( array( - 'taxonomy' => 'bar', - ) ); - $bar_term_2 = $this->factory->term->create( array( - 'taxonomy' => 'bar', - ) ); - - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - - wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); - wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); - wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); - wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); - wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); - wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - 'relation' => 'OR', - array( - 'taxonomy' => 'foo', - 'terms' => array( $foo_term_2 ), - 'field' => 'term_id', - ), - array( - 'relation' => 'AND', - array( - 'taxonomy' => 'foo', - 'terms' => array( $foo_term_1 ), - 'field' => 'term_id', - ), - array( - 'taxonomy' => 'bar', - 'terms' => array( $bar_term_1 ), - 'field' => 'term_id', - ), - ), - ), - ) ); - - _unregister_taxonomy( 'foo' ); - _unregister_taxonomy( 'bar' ); - - $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); - } - - /** - * @ticket 29738 - * @group taxonomy - */ - public function test_tax_query_one_double_nested_query_one_first_order_query() { - register_taxonomy( 'foo', 'post' ); - register_taxonomy( 'bar', 'post' ); - - $foo_term_1 = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $foo_term_2 = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $bar_term_1 = $this->factory->term->create( array( - 'taxonomy' => 'bar', - ) ); - $bar_term_2 = $this->factory->term->create( array( - 'taxonomy' => 'bar', - ) ); - - $p1 = $this->factory->post->create(); - $p2 = $this->factory->post->create(); - $p3 = $this->factory->post->create(); - $p4 = $this->factory->post->create(); - - wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); - wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); - wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); - wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); - wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); - wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); - - $q = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - 'relation' => 'OR', - array( - 'taxonomy' => 'foo', - 'terms' => array( $foo_term_2 ), - 'field' => 'term_id', - ), - array( - 'relation' => 'AND', - array( - 'taxonomy' => 'foo', - 'terms' => array( $foo_term_1 ), - 'field' => 'term_id', - ), - array( - 'relation' => 'OR', - array( - 'taxonomy' => 'bar', - 'terms' => array( $bar_term_1 ), - 'field' => 'term_id', - ), - array( - 'taxonomy' => 'bar', - 'terms' => array( $bar_term_2 ), - 'field' => 'term_id', - ), - ), - ), - ), - ) ); - - _unregister_taxonomy( 'foo' ); - _unregister_taxonomy( 'bar' ); - - $this->assertEqualSets( array( $p1, $p2, $p3 ), $q->posts ); - } - - /** - * @ticket 20604 - * @group taxonomy - */ - public function test_tax_query_relation_or_both_clauses_empty_terms() { - // An empty tax query should return an empty array, not all posts. - - $this->factory->post->create_many( 10 ); - - $query = new WP_Query( array( - 'fields' => 'ids', - 'update_post_term_cache' => false, - 'update_post_meta_cache' => false, - 'tax_query' => array( - 'relation' => 'OR', - array( - 'taxonomy' => 'post_tag', - 'field' => 'id', - 'terms' => false, - 'operator' => 'IN' - ), - array( - 'taxonomy' => 'category', - 'field' => 'id', - 'terms' => false, - 'operator' => 'IN' - ), - ) - ) ); - - $posts = $query->get_posts(); - $this->assertEquals( 0 , count( $posts ) ); - } - - /** - * @ticket 20604 - * @group taxonomy - */ - public function test_tax_query_relation_or_one_clause_empty_terms() { - // An empty tax query should return an empty array, not all posts. - - $this->factory->post->create_many( 10 ); - - $query = new WP_Query( array( - 'fields' => 'ids', - 'update_post_term_cache' => false, - 'update_post_meta_cache' => false, - 'tax_query' => array( - 'relation' => 'OR', - array( - 'taxonomy' => 'post_tag', - 'field' => 'id', - 'terms' => array( 'foo' ), - 'operator' => 'IN' - ), - array( - 'taxonomy' => 'category', - 'field' => 'id', - 'terms' => false, - 'operator' => 'IN' - ), - ) - ) ); - - $posts = $query->get_posts(); - $this->assertEquals( 0 , count( $posts ) ); - } - - /** - * @group taxonomy - */ - public function test_tax_query_include_children() { - $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) ); - $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) ); - $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'East Syndney', 'parent' => $cat_b ) ); - $cat_d = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'West Syndney', 'parent' => $cat_b ) ); - - $post_a = $this->factory->post->create( array( 'post_category' => array( $cat_a ) ) ); - $post_b = $this->factory->post->create( array( 'post_category' => array( $cat_b ) ) ); - $post_c = $this->factory->post->create( array( 'post_category' => array( $cat_c ) ) ); - $post_d = $this->factory->post->create( array( 'post_category' => array( $cat_d ) ) ); - - $posts = get_posts( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'field' => 'id', - 'terms' => array( $cat_a ), - ) - ) - ) ); - - $this->assertEquals( 4 , count( $posts ) ); - - $posts = get_posts( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'field' => 'id', - 'terms' => array( $cat_a ), - 'include_children' => false - ) - ) - ) ); - - $this->assertEquals( 1 , count( $posts ) ); - - $posts = get_posts( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'field' => 'id', - 'terms' => array( $cat_b ), - ) - ) - ) ); - - $this->assertEquals( 3 , count( $posts ) ); - - $posts = get_posts( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'field' => 'id', - 'terms' => array( $cat_b ), - 'include_children' => false - ) - ) - ) ); - - $this->assertEquals( 1 , count( $posts ) ); - - $posts = get_posts( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'field' => 'id', - 'terms' => array( $cat_c ), - ) - ) - ) ); - - $this->assertEquals( 1 , count( $posts ) ); - - $posts = get_posts( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'field' => 'id', - 'terms' => array( $cat_c ), - 'include_children' => false - ) - ) - ) ); - - $this->assertEquals( 1 , count( $posts ) ); - } - /** * @group taxonomy */ @@ -2662,160 +36,6 @@ class Tests_Post_Query extends WP_UnitTestCase { $this->assertEmpty( $posts2 ); } - /** - * @group taxonomy - */ - public function test_tax_query_taxonomy_with_attachments() { - $q = new WP_Query(); - - register_taxonomy_for_object_type( 'post_tag', 'attachment:image' ); - $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) ); - $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array( - 'post_mime_type' => 'image/jpeg', - 'post_type' => 'attachment' - ) ); - wp_set_object_terms( $image_id, $tag_id, 'post_tag' ); - - $posts = $q->query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'post_type' => 'attachment', - 'post_status' => 'inherit', - 'tax_query' => array( - array( - 'taxonomy' => 'post_tag', - 'field' => 'term_id', - 'terms' => array( $tag_id ) - ) - ) - ) ); - - $this->assertEquals( array( $image_id ), $posts ); - } - - /** - * @group taxonomy - */ - function test_tax_query_no_taxonomy() { - $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) ); - $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) ); - - $response1 = new WP_Query( array( - 'tax_query' => array( - array( 'terms' => array( $cat_id ) ) - ) - ) ); - $this->assertEmpty( $response1->posts ); - - $response2 = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'taxonomy' => 'category', - 'terms' => array( $cat_id ) - ) - ) - ) ); - $this->assertNotEmpty( $response2->posts ); - - $term = get_category( $cat_id ); - $response3 = new WP_Query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'tax_query' => array( - array( - 'field' => 'term_taxonomy_id', - 'terms' => array( $term->term_taxonomy_id ) - ) - ) - ) ); - $this->assertNotEmpty( $response3->posts ); - } - - /** - * @group taxonomy - */ - function test_term_taxonomy_id_field_no_taxonomy() { - $q = new WP_Query(); - - $posts = $this->factory->post->create_many( 5 ); - - $cats = $tags = array(); - - // need term_taxonomy_ids in addition to term_ids, so no factory - for ( $i = 0; $i < 5; $i++ ) { - $cats[$i] = wp_insert_term( 'category-' . $i , 'category' ); - $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' ); - - // post 0 gets all terms - wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true ); - wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true ); - } - - wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); - wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' ); - - wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' ); - wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); - - wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); - wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); - - $results1 = $q->query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'orderby' => 'ID', - 'order' => 'ASC', - 'tax_query' => array( - 'relation' => 'OR', - array( - 'field' => 'term_taxonomy_id', - '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'] ), - 'operator' => 'AND', - 'include_children' => false, - ), - array( - 'field' => 'term_taxonomy_id', - 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), - 'operator' => 'AND', - 'include_children' => false, - ) - ) - ) ); - - $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' ); - - $results2 = $q->query( array( - 'fields' => 'ids', - 'update_post_meta_cache' => false, - 'update_post_term_cache' => false, - 'orderby' => 'ID', - 'order' => 'ASC', - 'tax_query' => array( - 'relation' => 'AND', - array( - 'field' => 'term_taxonomy_id', - 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ), - 'operator' => 'IN', - 'include_children' => false, - ), - array( - 'field' => 'term_taxonomy_id', - 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), - 'operator' => 'IN', - 'include_children' => false, - ) - ) - ) ); - - $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' ); - } - /** * @ticket 28099 * @group taxonomy @@ -2846,204 +66,6 @@ class Tests_Post_Query extends WP_UnitTestCase { $this->assertNotEmpty( $q6 ); } - /** - * @group taxonomy - * @ticket 29738 - */ - public function test_populate_taxonomy_query_var_from_tax_query() { - register_taxonomy( 'foo', 'post' ); - $t = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $c = $this->factory->term->create( array( - 'taxonomy' => 'category', - ) ); - - $q = new WP_Query( array( - 'tax_query' => array( - // Empty terms mean that this one should be skipped - array( - 'taxonomy' => 'bar', - 'terms' => array(), - ), - - // Category and post tags should be skipped - array( - 'taxonomy' => 'category', - 'terms' => array( $c ), - ), - - array( - 'taxonomy' => 'foo', - 'terms' => array( $t ), - ), - ), - ) ); - - $this->assertSame( 'foo', $q->get( 'taxonomy' ) ); - - _unregister_taxonomy( 'foo' ); - } - - /** - * @group taxonomy - */ - public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() { - register_taxonomy( 'foo', 'post' ); - register_taxonomy( 'foo1', 'post' ); - $t = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - - $q = new WP_Query( array( - 'taxonomy' => 'bar', - 'tax_query' => array( - array( - 'taxonomy' => 'foo', - 'terms' => array( $t ), - ), - ), - ) ); - - $this->assertSame( 'bar', $q->get( 'taxonomy' ) ); - - _unregister_taxonomy( 'foo' ); - _unregister_taxonomy( 'foo1' ); - } - - /** - * @group taxonomy - */ - public function test_populate_term_query_var_from_tax_query() { - register_taxonomy( 'foo', 'post' ); - $t = $this->factory->term->create( array( - 'taxonomy' => 'foo', - 'slug' => 'bar', - ) ); - - $q = new WP_Query( array( - 'tax_query' => array( - array( - 'taxonomy' => 'foo', - 'terms' => array( 'bar' ), - 'field' => 'slug', - ), - ), - ) ); - - $this->assertSame( 'bar', $q->get( 'term' ) ); - - _unregister_taxonomy( 'foo' ); - } - - /** - * @group taxonomy - */ - public function test_populate_term_id_query_var_from_tax_query() { - register_taxonomy( 'foo', 'post' ); - $t = $this->factory->term->create( array( - 'taxonomy' => 'foo', - 'slug' => 'bar', - ) ); - - $q = new WP_Query( array( - 'tax_query' => array( - array( - 'taxonomy' => 'foo', - 'terms' => array( $t ), - 'field' => 'term_id', - ), - ), - ) ); - - $this->assertEquals( $t, $q->get( 'term_id' ) ); - - _unregister_taxonomy( 'foo' ); - } - - /** - * @group taxonomy - * @ticket 29738 - */ - public function test_populate_cat_category_name_query_var_from_tax_query() { - register_taxonomy( 'foo', 'post' ); - $t = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $c = $this->factory->term->create( array( - 'taxonomy' => 'category', - 'slug' => 'bar', - ) ); - - $q = new WP_Query( array( - 'tax_query' => array( - // Non-category should be skipped - array( - 'taxonomy' => 'foo', - 'terms' => array( $t ), - ), - - // Empty terms mean that this one should be skipped - array( - 'taxonomy' => 'category', - 'terms' => array(), - ), - - // Category and post tags should be skipped - array( - 'taxonomy' => 'category', - 'terms' => array( $c ), - ), - ), - ) ); - - $this->assertEquals( $c, $q->get( 'cat' ) ); - $this->assertEquals( 'bar', $q->get( 'category_name' ) ); - - _unregister_taxonomy( 'foo' ); - } - - /** - * @group taxonomy - * @ticket 29738 - */ - public function test_populate_tag_id_query_var_from_tax_query() { - register_taxonomy( 'foo', 'post' ); - $t = $this->factory->term->create( array( - 'taxonomy' => 'foo', - ) ); - $tag = $this->factory->term->create( array( - 'taxonomy' => 'post_tag', - 'slug' => 'bar', - ) ); - - $q = new WP_Query( array( - 'tax_query' => array( - // Non-tag should be skipped - array( - 'taxonomy' => 'foo', - 'terms' => array( $t ), - ), - - // Empty terms mean that this one should be skipped - array( - 'taxonomy' => 'post_tag', - 'terms' => array(), - ), - - // Category and post tags should be skipped - array( - 'taxonomy' => 'post_tag', - 'terms' => array( $tag ), - ), - ), - ) ); - - $this->assertEquals( $tag, $q->get( 'tag_id' ) ); - - _unregister_taxonomy( 'foo' ); - } - /** * @ticket 22448 */ diff --git a/tests/phpunit/tests/query/isTerm.php b/tests/phpunit/tests/query/isTerm.php new file mode 100644 index 0000000000..6ceff16974 --- /dev/null +++ b/tests/phpunit/tests/query/isTerm.php @@ -0,0 +1,283 @@ +init(); + $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); + + create_initial_taxonomies(); + register_taxonomy( 'testtax', 'post', array( 'public' => true ) ); + + $wp_rewrite->flush_rules(); + + $this->tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) ); + $this->cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) ); + $this->tax_id = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug' ) ); + $this->tax_id2 = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug2' ) ); + $this->post_id = $this->factory->post->create(); + wp_set_object_terms( $this->post_id, $this->cat_id, 'category' ); + wp_set_object_terms( $this->post_id, array( $this->tax_id, $this->tax_id2 ), 'testtax' ); + + $this->cat = get_term( $this->cat_id, 'category' ); + _make_cat_compat( $this->cat ); + $this->tag = get_term( $this->tag_id, 'post_tag' ); + + $this->uncat = get_term_by( 'slug', 'uncategorized', 'category' ); + _make_cat_compat( $this->uncat ); + + add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + } + + function tearDown() { + global $wp_rewrite; + parent::tearDown(); + + _unregister_taxonomy( 'testtax' ); + + $wp_rewrite->init(); + + remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + } + + function test_tag_action_tax() { + // tag with tax added + $this->go_to( home_url( "/tag/tag-slug/" ) ); + $this->assertQueryTrue( 'is_tag', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'tax_query' ) ); + $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); + $this->assertNotEmpty( get_query_var( 'term_id' ) ); + $this->assertNotEmpty( get_query_var( 'tag_id' ) ); + $this->assertEquals( get_queried_object(), $this->tag ); + } + + function test_tag_query_cat_action_tax() { + // tag + category with tax added + $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id" ) ); + $this->assertQueryTrue( 'is_category', 'is_tag', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'tax_query' ) ); + $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); + $this->assertNotEmpty( get_query_var( 'term_id' ) ); + $this->assertNotEmpty( get_query_var( 'cat' ) ); + $this->assertNotEmpty( get_query_var( 'tag_id' ) ); + $this->assertEquals( get_queried_object(), $this->cat ); + } + + function test_tag_query_cat_query_tax_action_tax() { + // tag + category + tax with tax added + $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id&testtax=tax-slug2" ) ); + $this->assertQueryTrue( 'is_category', 'is_tag', 'is_tax', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'tax_query' ) ); + $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); + $this->assertNotEmpty( get_query_var( 'term_id' ) ); + $this->assertNotEmpty( get_query_var( 'cat' ) ); + $this->assertNotEmpty( get_query_var( 'tag_id' ) ); + $this->assertNotEmpty( get_query_var( 'testtax' ) ); + $this->assertEquals( get_queried_object(), $this->cat ); + } + + function test_cat_action_tax() { + // category with tax added + $this->go_to( home_url( "/category/cat-slug/" ) ); + $this->assertQueryTrue( 'is_category', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'cat' ) ); + $this->assertNotEmpty( get_query_var( 'tax_query' ) ); + $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); + $this->assertNotEmpty( get_query_var( 'term_id' ) ); + $this->assertEquals( get_queried_object(), $this->cat ); + } + + /** + * @ticket 26627 + */ + function test_cat_uncat_action_tax() { + // category with tax added + add_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 ); + + $this->go_to( home_url( "/category/uncategorized/" ) ); + $this->assertQueryTrue( 'is_category', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'cat' ) ); + $this->assertNotEmpty( get_query_var( 'tax_query' ) ); + $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); + $this->assertNotEmpty( get_query_var( 'term_id' ) ); + $this->assertEquals( get_queried_object(), $this->uncat ); + + remove_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 ); + } + + function _cat_uncat_action_tax( &$query ) { + $this->assertTrue( $query->is_category() ); + $this->assertTrue( $query->is_archive() ); + $this->assertNotEmpty( $query->get( 'category_name' ) ); + $this->assertNotEmpty( $query->get( 'tax_query' ) ); + $this->assertEquals( $query->get_queried_object(), $this->uncat ); + } + + /** + * @ticket 26728 + */ + function test_tax_action_tax() { + // tax with tax added + $this->go_to( home_url( '/testtax/tax-slug2/' ) ); + $this->assertQueryTrue( 'is_tax', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'tax_query' ) ); + $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); + $this->assertNotEmpty( get_query_var( 'term_id' ) ); + $this->assertEquals( get_queried_object(), get_term( $this->tax_id, 'testtax' ) ); + } + + function test_tax_query_tag_action_tax() { + // tax + tag with tax added + $this->go_to( home_url( "/testtax/tax-slug2/?tag_id=$this->tag_id" ) ); + $this->assertQueryTrue( 'is_tag', 'is_tax', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'tax_query' ) ); + $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); + $this->assertNotEmpty( get_query_var( 'term_id' ) ); + $this->assertNotEmpty( get_query_var( 'tag_id' ) ); + $this->assertEquals( get_queried_object(), $this->tag ); + } + + function test_tax_query_cat_action_tax() { + // tax + cat with tax added + $this->go_to( home_url( "/testtax/tax-slug2/?cat=$this->cat_id" ) ); + $this->assertQueryTrue( 'is_category', 'is_tax', 'is_archive' ); + $this->assertNotEmpty( get_query_var( 'tax_query' ) ); + $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); + $this->assertNotEmpty( get_query_var( 'term_id' ) ); + $this->assertNotEmpty( get_query_var( 'cat' ) ); + $this->assertEquals( get_queried_object(), $this->cat ); + } + + function pre_get_posts_tax_category_tax_query( &$query ) { + $query->set( 'tax_query', array( + array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $this->tax_id ) + ) ); + } + + /** + * @group 30623 + */ + public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_term_id_should_return_term_object() { + // Don't override the args provided below. + remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + + $args = array( + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'testtax', + 'field' => 'term_id', + 'terms' => array( + $this->tax_id, + ), + ), + ) + ); + + $q = new WP_Query( $args ); + $object = $q->get_queried_object(); + + $expected = get_term( $this->tax_id, 'testtax' ); + + $this->assertEquals( $expected, $object ); + } + + /** + * @group 30623 + */ + public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_slug_should_return_term_object() { + // Don't override the args provided below. + remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + + $args = array( + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'testtax', + 'field' => 'slug', + 'terms' => array( + 'tax-slug', + ), + ), + ) + ); + + $q = new WP_Query( $args ); + $object = $q->get_queried_object(); + + $expected = get_term( $this->tax_id, 'testtax' ); + + // Only compare term_id because object_id may or may not be part of either value. + $this->assertEquals( $expected->term_id, $object->term_id ); + } + + /** + * @group 30623 + */ + public function test_get_queried_object_with_custom_taxonomy_tax_query_with_multiple_clauses_should_return_term_object_corresponding_to_the_first_queried_tax() { + // Don't override the args provided below. + remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + + register_taxonomy( 'testtax2', 'post' ); + $testtax2_term_id = $this->factory->term->create( array( + 'taxonomy' => 'testtax2', + 'slug' => 'testtax2-slug', + ) ); + + $args = array( + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'testtax', + 'field' => 'slug', + 'terms' => array( + 'tax-slug', + ), + ), + array( + 'taxonomy' => 'testtax2', + 'field' => 'slug', + 'terms' => array( + 'testtax2-slug', + ), + ), + ) + ); + + $q = new WP_Query( $args ); + $object = $q->get_queried_object(); + + $expected = get_term( $this->tax_id, 'testtax' ); + + // Only compare term_id because object_id may or may not be part of either value. + $this->assertEquals( $expected->term_id, $object->term_id ); + } +} diff --git a/tests/phpunit/tests/query/metaQuery.php b/tests/phpunit/tests/query/metaQuery.php new file mode 100644 index 0000000000..8f9b212d81 --- /dev/null +++ b/tests/phpunit/tests/query/metaQuery.php @@ -0,0 +1,1561 @@ +factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + add_post_meta( $p2, 'oof', 'bar' ); + add_post_meta( $p3, 'oof', 'baz' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'value' => 'bar', + ), + ), + ) ); + + $expected = array( $p1, $p2 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_no_value() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + add_post_meta( $p2, 'oof', 'bar' ); + add_post_meta( $p3, 'oof', 'baz' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'oof', + ), + ), + ) ); + + $expected = array( $p2, $p3 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_default() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'bar', + ), + ), + ) ); + + $expected = array( $p1 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_equals() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'bar', + 'compare' => '=', + ), + ), + ) ); + + $expected = array( $p1 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_not_equals() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + add_post_meta( $p2, 'foo', 'baz' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'bar', + 'compare' => '!=', + ), + ), + ) ); + + $expected = array( $p2 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_arithmetic_comparisons() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', '1' ); + add_post_meta( $p2, 'foo', '2' ); + add_post_meta( $p3, 'foo', '3' ); + + // < + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 2, + 'compare' => '<', + ), + ), + ) ); + + $expected = array( $p1 ); + $this->assertEqualSets( $expected, $query->posts ); + + // <= + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 2, + 'compare' => '<=', + ), + ), + ) ); + + $expected = array( $p1, $p2 ); + $this->assertEqualSets( $expected, $query->posts ); + + // >= + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 2, + 'compare' => '>=', + ), + ), + ) ); + + $expected = array( $p2, $p3 ); + $this->assertEqualSets( $expected, $query->posts ); + + // > + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 2, + 'compare' => '>', + ), + ), + ) ); + + $expected = array( $p3 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_like() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'ba', + 'compare' => 'LIKE', + ), + ), + ) ); + + $expected = array( $p1 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_not_like() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + add_post_meta( $p2, 'foo', 'rab' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'ba', + 'compare' => 'NOT LIKE', + ), + ), + ) ); + + $expected = array( $p2 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_between_not_between() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', '1' ); + add_post_meta( $p2, 'foo', '10' ); + add_post_meta( $p3, 'foo', '100' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => array( 9, 12 ), + 'compare' => 'BETWEEN', + 'type' => 'NUMERIC', + ), + ), + ) ); + + $expected = array( $p2 ); + $this->assertEqualSets( $expected, $query->posts ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => array( 9, 12 ), + 'compare' => 'NOT BETWEEN', + 'type' => 'NUMERIC', + ), + ), + ) ); + + $expected = array( $p1, $p3 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_regexp_rlike() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + add_post_meta( $p2, 'foo', 'baz' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'z$', + 'compare' => 'REGEXP', + ), + ), + ) ); + + $expected = array( $p2 ); + $this->assertEqualSets( $expected, $query->posts ); + + // RLIKE is a synonym for REGEXP. + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'z$', + 'compare' => 'RLIKE', + ), + ), + ) ); + + $expected = array( $p2 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_single_query_compare_not_regexp() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + add_post_meta( $p2, 'foo', 'baz' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'z$', + 'compare' => 'NOT REGEXP', + ), + ), + ) ); + + $expected = array( $p1 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_relation_default() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'foo value 1' ); + add_post_meta( $p1, 'bar', 'bar value 1' ); + add_post_meta( $p2, 'foo', 'foo value 1' ); + add_post_meta( $p2, 'bar', 'bar value 2' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo', + 'value' => 'foo value 1', + ), + array( + 'key' => 'bar', + 'value' => 'bar value 1', + ), + ), + ) ); + + $expected = array( $p1 ); + $this->assertEquals( $expected, $query->posts ); + } + + public function test_meta_query_relation_or() { + $post_id = $this->factory->post->create(); + add_post_meta( $post_id, 'foo', rand_str() ); + add_post_meta( $post_id, 'foo', rand_str() ); + $post_id2 = $this->factory->post->create(); + add_post_meta( $post_id2, 'bar', 'val2' ); + $post_id3 = $this->factory->post->create(); + add_post_meta( $post_id3, 'baz', rand_str() ); + $post_id4 = $this->factory->post->create(); + add_post_meta( $post_id4, 'froo', rand_str() ); + $post_id5 = $this->factory->post->create(); + add_post_meta( $post_id5, 'tango', 'val2' ); + $post_id6 = $this->factory->post->create(); + add_post_meta( $post_id6, 'bar', 'val1' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + array( + 'key' => 'foo' + ), + array( + 'key' => 'bar', + 'value' => 'val2' + ), + array( + 'key' => 'baz' + ), + array( + 'key' => 'froo' + ), + 'relation' => 'OR', + ), + ) ); + + $expected = array( $post_id, $post_id2, $post_id3, $post_id4 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_query_relation_and() { + $post_id = $this->factory->post->create(); + add_post_meta( $post_id, 'foo', rand_str() ); + add_post_meta( $post_id, 'foo', rand_str() ); + $post_id2 = $this->factory->post->create(); + add_post_meta( $post_id2, 'bar', 'val2' ); + add_post_meta( $post_id2, 'foo', rand_str() ); + $post_id3 = $this->factory->post->create(); + add_post_meta( $post_id3, 'baz', rand_str() ); + $post_id4 = $this->factory->post->create(); + add_post_meta( $post_id4, 'froo', rand_str() ); + $post_id5 = $this->factory->post->create(); + add_post_meta( $post_id5, 'tango', 'val2' ); + $post_id6 = $this->factory->post->create(); + add_post_meta( $post_id6, 'bar', 'val1' ); + add_post_meta( $post_id6, 'foo', rand_str() ); + $post_id7 = $this->factory->post->create(); + add_post_meta( $post_id7, 'foo', rand_str() ); + add_post_meta( $post_id7, 'froo', rand_str() ); + add_post_meta( $post_id7, 'baz', rand_str() ); + add_post_meta( $post_id7, 'bar', 'val2' ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'foo' + ), + array( + 'key' => 'bar', + 'value' => 'val2' + ), + array( + 'key' => 'baz' + ), + array( + 'key' => 'froo' + ), + 'relation' => 'AND', + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $post_id7 ); + $this->assertEqualSets( $expected, $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'foo' + ), + array( + 'key' => 'bar', + ), + 'relation' => 'AND', + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $post_id2, $post_id6, $post_id7 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 30681 + */ + public function test_meta_query_compare_exists() { + $posts = $this->factory->post->create_many( 3 ); + add_post_meta( $posts[0], 'foo', 'bar' ); + add_post_meta( $posts[2], 'foo', 'baz' ); + + $query = new WP_Query( array( + 'fields' => 'ids', + 'meta_query' => array( + array( + 'compare' => 'EXISTS', + 'key' => 'foo', + ), + ), + ) ); + + $this->assertEqualSets( array( $posts[0], $posts[2] ), $query->posts ); + } + + /** + * @ticket 30681 + */ + public function test_meta_query_compare_exists_with_value_should_convert_to_equals() { + $posts = $this->factory->post->create_many( 3 ); + add_post_meta( $posts[0], 'foo', 'bar' ); + add_post_meta( $posts[2], 'foo', 'baz' ); + + $query = new WP_Query( array( + 'fields' => 'ids', + 'meta_query' => array( + array( + 'compare' => 'EXISTS', + 'value' => 'baz', + 'key' => 'foo', + ), + ), + ) ); + + $this->assertEqualSets( array( $posts[2] ), $query->posts ); + } + + /** + * @ticket 30681 + */ + public function test_meta_query_compare_not_exists_should_ignore_value() { + $posts = $this->factory->post->create_many( 3 ); + add_post_meta( $posts[0], 'foo', 'bar' ); + add_post_meta( $posts[2], 'foo', 'baz' ); + + $query = new WP_Query( array( + 'fields' => 'ids', + 'meta_query' => array( + array( + 'compare' => 'NOT EXISTS', + 'value' => 'bar', + 'key' => 'foo', + ), + ), + ) ); + + $this->assertEqualSets( array( $posts[1] ), $query->posts ); + } + + /** + * @ticket 18158 + */ + public function test_meta_query_compare_not_exists() { + $post_id = $this->factory->post->create(); + add_post_meta( $post_id, 'foo', rand_str() ); + $post_id2 = $this->factory->post->create(); + add_post_meta( $post_id2, 'bar', rand_str() ); + $post_id3 = $this->factory->post->create(); + add_post_meta( $post_id3, 'bar', rand_str() ); + $post_id4 = $this->factory->post->create(); + add_post_meta( $post_id4, 'baz', rand_str() ); + $post_id5 = $this->factory->post->create(); + add_post_meta( $post_id5, 'foo', rand_str() ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'foo', + 'compare' => 'NOT EXISTS', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $post_id2, $post_id3, $post_id4 ); + $this->assertEqualSets( $expected, $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'foo', + 'compare' => 'NOT EXISTS', + ), + array( + 'key' => 'bar', + 'compare' => 'NOT EXISTS', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $post_id4 ); + $this->assertEquals( $expected, $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'foo', + 'compare' => 'NOT EXISTS', + ), + array( + 'key' => 'bar', + 'compare' => 'NOT EXISTS', + ), + array( + 'key' => 'baz', + 'compare' => 'NOT EXISTS', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $this->assertEquals( 0, count( $query->posts ) ); + } + + /** + * @ticket 29062 + */ + public function test_meta_query_compare_not_exists_with_another_condition_relation_or() { + $posts = $this->factory->post->create_many( 4 ); + update_post_meta( $posts[0], 'color', 'orange' ); + update_post_meta( $posts[1], 'color', 'blue' ); + update_post_meta( $posts[1], 'vegetable', 'onion' ); + update_post_meta( $posts[2], 'vegetable', 'shallot' ); + + $post_3_meta = get_post_meta( $posts[3] ); + foreach ( $post_3_meta as $meta_key => $meta_value ) { + delete_post_meta( $posts[3], $meta_key ); + } + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'vegetable', + 'value' => 'onion', + ), + array( + 'key' => 'color', + 'compare' => 'NOT EXISTS', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[1], $posts[2], $posts[3] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_or_compare_equals() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'vegetable', + 'value' => 'onion', + 'compare' => '=', + ), + array( + 'key' => 'vegetable', + 'value' => 'shallot', + 'compare' => '=', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[1], $posts[2] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_or_compare_equals_different_keys() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'vegetable', + 'value' => 'onion', + 'compare' => '=', + ), + array( + 'key' => 'color', + 'value' => 'orange', + 'compare' => '=', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[0], $posts[1] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_or_compare_equals_and_in() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'vegetable', + 'value' => 'onion', + 'compare' => '=', + ), + array( + 'key' => 'color', + 'value' => array( 'orange', 'green' ), + 'compare' => 'IN', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[0], $posts[1] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_or_compare_equals_and_like() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'vegetable', + 'value' => 'onion', + 'compare' => '=', + ), + array( + 'key' => 'vegetable', + 'value' => 'hall', + 'compare' => 'LIKE', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[1], $posts[2] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_or_compare_equals_and_between() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'number_of_colors', '2' ); + add_post_meta( $posts[1], 'number_of_colors', '5' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'vegetable', + 'value' => 'shallot', + 'compare' => '=', + ), + array( + 'key' => 'number_of_colors', + 'value' => array( 1, 3 ), + 'compare' => 'BETWEEN', + 'type' => 'SIGNED', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[0], $posts[2] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_and_compare_in_same_keys() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + add_post_meta( $posts[3], 'vegetable', 'banana' ); + add_post_meta( $posts[3], 'vegetable', 'onion' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'vegetable', + 'value' => array( 'onion', 'shallot' ), + 'compare' => 'IN', + ), + array( + 'key' => 'vegetable', + 'value' => array( 'banana' ), + 'compare' => 'IN', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[3] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_and_compare_in_different_keys() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[1], 'vegetable', 'shallot' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + add_post_meta( $posts[3], 'vegetable', 'banana' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'vegetable', + 'value' => array( 'onion', 'shallot' ), + 'compare' => 'IN', + ), + array( + 'key' => 'color', + 'value' => array( 'blue' ), + 'compare' => 'IN', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[1] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_and_compare_not_equals() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + add_post_meta( $posts[3], 'vegetable', 'banana' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'vegetable', + 'value' => 'onion', + 'compare' => '!=', + ), + array( + 'key' => 'vegetable', + 'value' => 'shallot', + 'compare' => '!=', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[3] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_and_compare_not_equals_different_keys() { + $posts = $this->factory->post->create_many( 4 ); + + // !shallot, but orange. + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[0], 'vegetable', 'onion' ); + + // !orange, but shallot. + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'shallot' ); + + // Neither. + add_post_meta( $posts[2], 'color', 'blue' ); + add_post_meta( $posts[2], 'vegetable', 'onion' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'vegetable', + 'value' => 'shallot', + 'compare' => '!=', + ), + array( + 'key' => 'color', + 'value' => 'orange', + 'compare' => '!=', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[2] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_and_compare_not_equals_not_in() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + add_post_meta( $posts[3], 'vegetable', 'banana' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'vegetable', + 'value' => 'onion', + 'compare' => '!=', + ), + array( + 'key' => 'vegetable', + 'value' => array( 'shallot' ), + 'compare' => 'NOT IN', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[3] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 24093 + */ + public function test_meta_query_relation_and_compare_not_equals_and_not_like() { + $posts = $this->factory->post->create_many( 4 ); + add_post_meta( $posts[0], 'color', 'orange' ); + add_post_meta( $posts[1], 'color', 'blue' ); + add_post_meta( $posts[1], 'vegetable', 'onion' ); + add_post_meta( $posts[2], 'vegetable', 'shallot' ); + add_post_meta( $posts[3], 'vegetable', 'banana' ); + + $query = new WP_Query( array( + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'vegetable', + 'value' => 'onion', + 'compare' => '!=', + ), + array( + 'key' => 'vegetable', + 'value' => 'hall', + 'compare' => 'NOT LIKE', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $expected = array( $posts[3] ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 23033 + */ + public function test_meta_query_decimal_results() { + $post_1 = $this->factory->post->create(); + $post_2 = $this->factory->post->create(); + $post_3 = $this->factory->post->create(); + $post_4 = $this->factory->post->create(); + + update_post_meta( $post_1, 'decimal_value', '-0.3' ); + update_post_meta( $post_2, 'decimal_value', '0.23409844' ); + update_post_meta( $post_3, 'decimal_value', '0.3' ); + update_post_meta( $post_4, 'decimal_value', '0.4' ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '.300', + 'compare' => '=', + 'type' => 'DECIMAL(10,2)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_3 ), $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '0.35', + 'compare' => '>', + 'type' => 'DECIMAL(10,2)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_4 ), $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '0.3', + 'compare' => '>=', + 'type' => 'DECIMAL(10,2)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_3, $post_4 ), $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '0', + 'compare' => '<', + 'type' => 'DECIMAL(10,2)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_1 ), $query->posts, 'ID' ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '0.3', + 'compare' => '<=', + 'type' => 'DECIMAL(10,2)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_1, $post_2, $post_3 ), $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => array( 0.23409845, .31 ), + 'compare' => 'BETWEEN', + 'type' => 'DECIMAL(10, 10)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_3 ), $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => array( 0.23409845, .31 ), + 'compare' => 'NOT BETWEEN', + 'type' => 'DECIMAL(10,10)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_1, $post_2, $post_4 ), $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '.3', + 'compare' => 'LIKE', + 'type' => 'DECIMAL(10,2)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_1, $post_3 ), $query->posts ); + + $query = new WP_Query( array( + 'meta_query' => array( + array( + 'key' => 'decimal_value', + 'value' => '.3', + 'compare' => 'NOT LIKE', + 'type' => 'DECIMAL(10,2)' + ) + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_2, $post_4 ), $query->posts ); + + $query = new WP_Query( array( + 'orderby' => 'meta_value', + 'order' => 'DESC', + 'meta_key' => 'decimal_value', + 'meta_type' => 'DECIMAL(10, 2)', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + $this->assertEqualSets( array( $post_4, $post_3, $post_2, $post_1 ), $query->posts ); + } + + /** + * @ticket 29604 + */ + public function test_meta_query_with_orderby_meta_value_relation_or() { + $posts = $this->factory->post->create_many( 4 ); + update_post_meta( $posts[0], 'foo', 5 ); + update_post_meta( $posts[1], 'foo', 6 ); + update_post_meta( $posts[2], 'foo', 4 ); + update_post_meta( $posts[3], 'foo', 7 ); + + update_post_meta( $posts[0], 'bar1', 'baz' ); + update_post_meta( $posts[1], 'bar1', 'baz' ); + update_post_meta( $posts[2], 'bar2', 'baz' ); + + $query = new WP_Query( array( + 'orderby' => 'meta_value', + 'order' => 'ASC', + 'meta_key' => 'foo', + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'bar1', + 'value' => 'baz', + 'compare' => '=', + ), + array( + 'key' => 'bar2', + 'value' => 'baz', + 'compare' => '=', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $query->posts ); + } + + /** + * @ticket 29604 + */ + public function test_meta_query_with_orderby_meta_value_relation_and() { + $posts = $this->factory->post->create_many( 4 ); + update_post_meta( $posts[0], 'foo', 5 ); + update_post_meta( $posts[1], 'foo', 6 ); + update_post_meta( $posts[2], 'foo', 4 ); + update_post_meta( $posts[3], 'foo', 7 ); + + update_post_meta( $posts[0], 'bar1', 'baz' ); + update_post_meta( $posts[1], 'bar1', 'baz' ); + update_post_meta( $posts[2], 'bar1', 'baz' ); + update_post_meta( $posts[3], 'bar1', 'baz' ); + update_post_meta( $posts[0], 'bar2', 'baz' ); + update_post_meta( $posts[1], 'bar2', 'baz' ); + update_post_meta( $posts[2], 'bar2', 'baz' ); + + $query = new WP_Query( array( + 'orderby' => 'meta_value', + 'order' => 'ASC', + 'meta_key' => 'foo', + 'meta_query' => array( + 'relation' => 'AND', + array( + 'key' => 'bar1', + 'value' => 'baz', + 'compare' => '=', + ), + array( + 'key' => 'bar2', + 'value' => 'baz', + 'compare' => '=', + ), + ), + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'fields' => 'ids', + ) ); + + $this->assertEquals( array( $posts[2], $posts[0], $posts[1] ), $query->posts ); + } + + /** + * @ticket 29642 + */ + public function test_meta_query_nested() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + add_post_meta( $p2, 'foo2', 'bar' ); + add_post_meta( $p3, 'foo2', 'bar' ); + add_post_meta( $p3, 'foo3', 'bar' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_term_meta_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'foo', + 'value' => 'bar', + ), + array( + 'relation' => 'AND', + array( + 'key' => 'foo2', + 'value' => 'bar', + ), + array( + 'key' => 'foo3', + 'value' => 'bar', + ), + ), + ), + ) ); + + $expected = array( $p1, $p3 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + /** + * @ticket 29642 + */ + public function test_meta_query_nested_two_levels_deep() { + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + add_post_meta( $p1, 'foo', 'bar' ); + add_post_meta( $p3, 'foo2', 'bar' ); + add_post_meta( $p3, 'foo3', 'bar' ); + add_post_meta( $p3, 'foo4', 'bar' ); + + $query = new WP_Query( array( + 'update_post_meta_cache' => false, + 'update_term_meta_cache' => false, + 'fields' => 'ids', + 'meta_query' => array( + 'relation' => 'OR', + array( + 'key' => 'foo', + 'value' => 'bar', + ), + array( + 'relation' => 'OR', + array( + 'key' => 'foo2', + 'value' => 'bar', + ), + array( + 'relation' => 'AND', + array( + 'key' => 'foo3', + 'value' => 'bar', + ), + array( + 'key' => 'foo4', + 'value' => 'bar', + ), + ), + ), + ), + ) ); + + $expected = array( $p1, $p3 ); + $this->assertEqualSets( $expected, $query->posts ); + } + + public function test_meta_between_not_between() { + $post_id = $this->factory->post->create(); + add_post_meta( $post_id, 'time', 500 ); + $post_id2 = $this->factory->post->create(); + add_post_meta( $post_id2, 'time', 1001 ); + $post_id3 = $this->factory->post->create(); + add_post_meta( $post_id3, 'time', 0 ); + $post_id4 = $this->factory->post->create(); + add_post_meta( $post_id4, 'time', 1 ); + $post_id5 = $this->factory->post->create(); + add_post_meta( $post_id5, 'time', 1000 ); + + $args = array( + 'meta_key' => 'time', + 'meta_value' => array( 1, 1000 ), + 'meta_type' => 'numeric', + 'meta_compare' => 'NOT BETWEEN' + ); + + $query = new WP_Query( $args ); + $this->assertEquals( 2, count ( $query->posts ) ); + foreach ( $query->posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $posts = wp_list_pluck( $query->posts, 'ID' ); + $this->assertEqualSets( array( $post_id2, $post_id3 ), $posts ); + + $args = array( + 'meta_key' => 'time', + 'meta_value' => array( 1, 1000 ), + 'meta_type' => 'numeric', + 'meta_compare' => 'BETWEEN' + ); + + $query = new WP_Query( $args ); + $this->assertEquals( 3, count ( $query->posts ) ); + foreach ( $query->posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $posts = wp_list_pluck( $query->posts, 'ID' ); + $this->assertEqualSets( array( $post_id, $post_id4, $post_id5 ), $posts ); + } + + /** + * @ticket 16829 + */ + public function test_meta_default_compare() { + // compare should default to IN when meta_value is an array + $post_id = $this->factory->post->create(); + add_post_meta( $post_id, 'foo', 'bar' ); + $post_id2 = $this->factory->post->create(); + add_post_meta( $post_id2, 'bar', 'baz' ); + $post_id3 = $this->factory->post->create(); + add_post_meta( $post_id3, 'foo', 'baz' ); + $post_id4 = $this->factory->post->create(); + add_post_meta( $post_id4, 'baz', 'bar' ); + $post_id5 = $this->factory->post->create(); + add_post_meta( $post_id5, 'foo', rand_str() ); + + $posts = get_posts( array( + 'meta_key' => 'foo', + 'meta_value' => array( 'bar', 'baz' ) + ) ); + + $this->assertEquals( 2, count( $posts ) ); + $posts = wp_list_pluck( $posts, 'ID' ); + $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); + + $posts = get_posts( array( + 'meta_key' => 'foo', + 'meta_value' => array( 'bar', 'baz' ), + 'meta_compare' => 'IN' + ) ); + + $this->assertEquals( 2, count( $posts ) ); + foreach ( $posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $posts = wp_list_pluck( $posts, 'ID' ); + $this->assertEqualSets( array( $post_id, $post_id3 ), $posts ); + } + + /** + * @ticket 17264 + */ + public function test_duplicate_posts_when_no_key() { + $post_id = $this->factory->post->create(); + add_post_meta( $post_id, 'city', 'Lorem' ); + add_post_meta( $post_id, 'address', '123 Lorem St.' ); + $post_id2 = $this->factory->post->create(); + add_post_meta( $post_id2, 'city', 'Lorem' ); + $post_id3 = $this->factory->post->create(); + add_post_meta( $post_id3, 'city', 'Loren' ); + + $args = array( + 'meta_query' => array( + array( + 'value' => 'lorem', + 'compare' => 'LIKE' + ) + ) + ); + + $posts = get_posts( $args ); + $this->assertEquals( 2, count( $posts ) ); + foreach ( $posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $posts = wp_list_pluck( $posts, 'ID' ); + $this->assertEqualSets( array( $post_id, $post_id2 ), $posts ); + } + + /** + * @ticket 15292 + */ + public function test_empty_meta_value() { + $post_id = $this->factory->post->create(); + add_post_meta( $post_id, 'foo', '0' ); + add_post_meta( $post_id, 'bar', 0 ); + $post_id2 = $this->factory->post->create(); + add_post_meta( $post_id2, 'foo', 1 ); + $post_id3 = $this->factory->post->create(); + add_post_meta( $post_id3, 'baz', 0 ); + $post_id4 = $this->factory->post->create(); + add_post_meta( $post_id4, 'baz', 0 ); + $post_id5 = $this->factory->post->create(); + add_post_meta( $post_id5, 'baz', 0 ); + add_post_meta( $post_id5, 'bar', '0' ); + $post_id6 = $this->factory->post->create(); + add_post_meta( $post_id6, 'baz', 0 ); + + $q = new WP_Query( array( 'meta_key' => 'foo', 'meta_value' => '0' ) ); + $this->assertEquals( 1, count ( $q->posts ) ); + foreach ( $q->posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $this->assertEquals( $post_id, $q->posts[0]->ID ); + + $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => '0' ) ); + $this->assertEquals( 2, count ( $posts ) ); + foreach ( $posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $posts = wp_list_pluck( $posts, 'ID' ); + $this->assertEqualSets( array( $post_id, $post_id5 ), $posts ); + + $posts = get_posts( array( 'meta_key' => 'bar', 'meta_value' => 0 ) ); + $this->assertEquals( 2, count ( $posts ) ); + foreach ( $posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $posts = wp_list_pluck( $posts, 'ID' ); + $this->assertEqualSets( array( $post_id, $post_id5 ), $posts ); + + $posts = get_posts( array( 'meta_value' => 0 ) ); + $this->assertEquals( 5, count ( $posts ) ); + foreach ( $posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $posts = wp_list_pluck( $posts, 'ID' ); + $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts ); + + $posts = get_posts( array( 'meta_value' => '0' ) ); + $this->assertEquals( 5, count ( $posts ) ); + foreach ( $posts as $post ) { + $this->assertInstanceOf( 'WP_Post', $post ); + $this->assertEquals( 'raw', $post->filter ); + } + $posts = wp_list_pluck( $posts, 'ID' ); + $this->assertEqualSets( array( $post_id, $post_id3, $post_id4, $post_id5, $post_id6 ), $posts ); + } +} diff --git a/tests/phpunit/tests/query/taxQuery.php b/tests/phpunit/tests/query/taxQuery.php index 61b08d951c..eed12ac371 100644 --- a/tests/phpunit/tests/query/taxQuery.php +++ b/tests/phpunit/tests/query/taxQuery.php @@ -1,282 +1,1308 @@ init(); - $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); - - create_initial_taxonomies(); - register_taxonomy( 'testtax', 'post', array( 'public' => true ) ); - - $wp_rewrite->flush_rules(); - - $this->tag_id = $this->factory->tag->create( array( 'slug' => 'tag-slug' ) ); - $this->cat_id = $this->factory->category->create( array( 'slug' => 'cat-slug' ) ); - $this->tax_id = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug' ) ); - $this->tax_id2 = $this->factory->term->create( array( 'taxonomy' => 'testtax', 'slug' => 'tax-slug2' ) ); - $this->post_id = $this->factory->post->create(); - wp_set_object_terms( $this->post_id, $this->cat_id, 'category' ); - wp_set_object_terms( $this->post_id, array( $this->tax_id, $this->tax_id2 ), 'testtax' ); - - $this->cat = get_term( $this->cat_id, 'category' ); - _make_cat_compat( $this->cat ); - $this->tag = get_term( $this->tag_id, 'post_tag' ); - - $this->uncat = get_term_by( 'slug', 'uncategorized', 'category' ); - _make_cat_compat( $this->uncat ); - - add_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); - } - - function tearDown() { - global $wp_rewrite; - parent::tearDown(); - - _unregister_taxonomy( 'testtax' ); - - $wp_rewrite->init(); - - remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); - } - - function test_tag_action_tax() { - // tag with tax added - $this->go_to( home_url( "/tag/tag-slug/" ) ); - $this->assertQueryTrue( 'is_tag', 'is_archive' ); - $this->assertNotEmpty( get_query_var( 'tax_query' ) ); - $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); - $this->assertNotEmpty( get_query_var( 'term_id' ) ); - $this->assertNotEmpty( get_query_var( 'tag_id' ) ); - $this->assertEquals( get_queried_object(), $this->tag ); - } - - function test_tag_query_cat_action_tax() { - // tag + category with tax added - $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id" ) ); - $this->assertQueryTrue( 'is_category', 'is_tag', 'is_archive' ); - $this->assertNotEmpty( get_query_var( 'tax_query' ) ); - $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); - $this->assertNotEmpty( get_query_var( 'term_id' ) ); - $this->assertNotEmpty( get_query_var( 'cat' ) ); - $this->assertNotEmpty( get_query_var( 'tag_id' ) ); - $this->assertEquals( get_queried_object(), $this->cat ); - } - - function test_tag_query_cat_query_tax_action_tax() { - // tag + category + tax with tax added - $this->go_to( home_url( "/tag/tag-slug/?cat=$this->cat_id&testtax=tax-slug2" ) ); - $this->assertQueryTrue( 'is_category', 'is_tag', 'is_tax', 'is_archive' ); - $this->assertNotEmpty( get_query_var( 'tax_query' ) ); - $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); - $this->assertNotEmpty( get_query_var( 'term_id' ) ); - $this->assertNotEmpty( get_query_var( 'cat' ) ); - $this->assertNotEmpty( get_query_var( 'tag_id' ) ); - $this->assertNotEmpty( get_query_var( 'testtax' ) ); - $this->assertEquals( get_queried_object(), $this->cat ); - } - - function test_cat_action_tax() { - // category with tax added - $this->go_to( home_url( "/category/cat-slug/" ) ); - $this->assertQueryTrue( 'is_category', 'is_archive' ); - $this->assertNotEmpty( get_query_var( 'cat' ) ); - $this->assertNotEmpty( get_query_var( 'tax_query' ) ); - $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); - $this->assertNotEmpty( get_query_var( 'term_id' ) ); - $this->assertEquals( get_queried_object(), $this->cat ); - } - - /** - * @ticket 26627 - */ - function test_cat_uncat_action_tax() { - // category with tax added - add_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 ); - - $this->go_to( home_url( "/category/uncategorized/" ) ); - $this->assertQueryTrue( 'is_category', 'is_archive' ); - $this->assertNotEmpty( get_query_var( 'cat' ) ); - $this->assertNotEmpty( get_query_var( 'tax_query' ) ); - $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); - $this->assertNotEmpty( get_query_var( 'term_id' ) ); - $this->assertEquals( get_queried_object(), $this->uncat ); - - remove_action( 'pre_get_posts', array( $this, '_cat_uncat_action_tax' ), 11 ); - } - - function _cat_uncat_action_tax( &$query ) { - $this->assertTrue( $query->is_category() ); - $this->assertTrue( $query->is_archive() ); - $this->assertNotEmpty( $query->get( 'category_name' ) ); - $this->assertNotEmpty( $query->get( 'tax_query' ) ); - $this->assertEquals( $query->get_queried_object(), $this->uncat ); - } - - /** - * @ticket 26728 - */ - function test_tax_action_tax() { - // tax with tax added - $this->go_to( home_url( '/testtax/tax-slug2/' ) ); - $this->assertQueryTrue( 'is_tax', 'is_archive' ); - $this->assertNotEmpty( get_query_var( 'tax_query' ) ); - $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); - $this->assertNotEmpty( get_query_var( 'term_id' ) ); - $this->assertEquals( get_queried_object(), get_term( $this->tax_id, 'testtax' ) ); - } - - function test_tax_query_tag_action_tax() { - // tax + tag with tax added - $this->go_to( home_url( "/testtax/tax-slug2/?tag_id=$this->tag_id" ) ); - $this->assertQueryTrue( 'is_tag', 'is_tax', 'is_archive' ); - $this->assertNotEmpty( get_query_var( 'tax_query' ) ); - $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); - $this->assertNotEmpty( get_query_var( 'term_id' ) ); - $this->assertNotEmpty( get_query_var( 'tag_id' ) ); - $this->assertEquals( get_queried_object(), $this->tag ); - } - - function test_tax_query_cat_action_tax() { - // tax + cat with tax added - $this->go_to( home_url( "/testtax/tax-slug2/?cat=$this->cat_id" ) ); - $this->assertQueryTrue( 'is_category', 'is_tax', 'is_archive' ); - $this->assertNotEmpty( get_query_var( 'tax_query' ) ); - $this->assertNotEmpty( get_query_var( 'taxonomy' ) ); - $this->assertNotEmpty( get_query_var( 'term_id' ) ); - $this->assertNotEmpty( get_query_var( 'cat' ) ); - $this->assertEquals( get_queried_object(), $this->cat ); - } - - function pre_get_posts_tax_category_tax_query( &$query ) { - $query->set( 'tax_query', array( - array( 'taxonomy' => 'testtax', 'field' => 'term_id', 'terms' => $this->tax_id ) + public function test_tax_query_single_query_single_term_field_slug() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + wp_set_post_terms( $p1, $t, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo' ), + 'field' => 'slug', + ), + ), + ) ); + + $this->assertEquals( array( $p1 ), $q->posts ); } - /** - * @group 30623 - */ - public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_term_id_should_return_term_object() { - // Don't override the args provided below. - remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); + public function test_tax_query_single_query_single_term_field_name() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); - $args = array( + wp_set_post_terms( $p1, $t, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, 'tax_query' => array( - 'relation' => 'AND', array( - 'taxonomy' => 'testtax', + 'taxonomy' => 'category', + 'terms' => array( 'Foo' ), + 'field' => 'name', + ), + ), + ) ); + + $this->assertEquals( array( $p1 ), $q->posts ); + } + + public function test_tax_query_single_query_single_term_field_term_taxonomy_id() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + $tt_ids = wp_set_post_terms( $p1, $t, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => $tt_ids, + 'field' => 'term_taxonomy_id', + ), + ), + ) ); + + $this->assertEquals( array( $p1 ), $q->posts ); + } + + public function test_tax_query_single_query_single_term_field_term_id() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + wp_set_post_terms( $p1, $t, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( $t ), 'field' => 'term_id', - 'terms' => array( - $this->tax_id, - ), ), - ) - ); - - $q = new WP_Query( $args ); - $object = $q->get_queried_object(); - - $expected = get_term( $this->tax_id, 'testtax' ); - - $this->assertEquals( $expected, $object ); - } - - /** - * @group 30623 - */ - public function test_get_queried_object_with_custom_taxonomy_tax_query_and_field_slug_should_return_term_object() { - // Don't override the args provided below. - remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); - - $args = array( - 'tax_query' => array( - 'relation' => 'AND', - array( - 'taxonomy' => 'testtax', - 'field' => 'slug', - 'terms' => array( - 'tax-slug', - ), - ), - ) - ); - - $q = new WP_Query( $args ); - $object = $q->get_queried_object(); - - $expected = get_term( $this->tax_id, 'testtax' ); - - // Only compare term_id because object_id may or may not be part of either value. - $this->assertEquals( $expected->term_id, $object->term_id ); - } - - /** - * @group 30623 - */ - public function test_get_queried_object_with_custom_taxonomy_tax_query_with_multiple_clauses_should_return_term_object_corresponding_to_the_first_queried_tax() { - // Don't override the args provided below. - remove_action( 'pre_get_posts', array( $this, 'pre_get_posts_tax_category_tax_query' ) ); - - register_taxonomy( 'testtax2', 'post' ); - $testtax2_term_id = $this->factory->term->create( array( - 'taxonomy' => 'testtax2', - 'slug' => 'testtax2-slug', + ), ) ); - $args = array( + $this->assertEquals( array( $p1 ), $q->posts ); + } + + public function test_tax_query_single_query_single_term_operator_in() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + wp_set_post_terms( $p1, $t, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo' ), + 'field' => 'slug', + 'operator' => 'IN', + ), + ), + ) ); + + $this->assertEquals( array( $p1 ), $q->posts ); + } + + public function test_tax_query_single_query_single_term_operator_not_in() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + wp_set_post_terms( $p1, $t, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo' ), + 'field' => 'slug', + 'operator' => 'NOT IN', + ), + ), + ) ); + + $this->assertEquals( array( $p2 ), $q->posts ); + } + + public function test_tax_query_single_query_single_term_operator_and() { + $t = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + + wp_set_post_terms( $p1, $t, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo' ), + 'field' => 'slug', + 'operator' => 'AND', + ), + ), + ) ); + + $this->assertEquals( array( $p1 ), $q->posts ); + } + + public function test_tax_query_single_query_multiple_terms_operator_in() { + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'bar', + 'name' => 'Bar', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_post_terms( $p1, $t1, 'category' ); + wp_set_post_terms( $p2, $t2, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo', 'bar' ), + 'field' => 'slug', + 'operator' => 'IN', + ), + ), + ) ); + + $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); + } + + public function test_tax_query_single_query_multiple_terms_operator_not_in() { + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'bar', + 'name' => 'Bar', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_post_terms( $p1, $t1, 'category' ); + wp_set_post_terms( $p2, $t2, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo', 'bar' ), + 'field' => 'slug', + 'operator' => 'NOT IN', + ), + ), + ) ); + + $this->assertEquals( array( $p3 ), $q->posts ); + } + + /** + * @ticket 18105 + */ + public function test_tax_query_single_query_multiple_queries_operator_not_in() { + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'bar', + 'name' => 'Bar', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_post_terms( $p1, $t1, 'category' ); + wp_set_post_terms( $p2, $t2, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, 'tax_query' => array( 'relation' => 'AND', array( - 'taxonomy' => 'testtax', + 'taxonomy' => 'category', + 'terms' => array( 'foo' ), 'field' => 'slug', - 'terms' => array( - 'tax-slug', + 'operator' => 'NOT IN', + ), + array( + 'taxonomy' => 'category', + 'terms' => array( 'bar' ), + 'field' => 'slug', + 'operator' => 'NOT IN', + ), + ), + ) ); + + $this->assertEquals( array( $p3 ), $q->posts ); + } + + public function test_tax_query_single_query_multiple_terms_operator_and() { + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'bar', + 'name' => 'Bar', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, $t1, 'category' ); + wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo', 'bar' ), + 'field' => 'slug', + 'operator' => 'AND', + ), + ), + ) ); + + $this->assertEquals( array( $p2 ), $q->posts ); + } + + /** + * @ticket 29181 + */ + public function test_tax_query_operator_not_exists() { + register_taxonomy( 'wptests_tax1', 'post' ); + register_taxonomy( 'wptests_tax2', 'post' ); + + $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); + $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); + + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); + wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'orderby' => 'ID', + 'order' => 'ASC', + 'tax_query' => array( + array( + 'taxonomy' => 'wptests_tax2', + 'operator' => 'NOT EXISTS', + ), + ), + ) ); + + $this->assertEqualSets( array( $p1, $p3 ), $q->posts ); + } + + /** + * @ticket 29181 + */ + public function test_tax_query_operator_exists() { + register_taxonomy( 'wptests_tax1', 'post' ); + register_taxonomy( 'wptests_tax2', 'post' ); + + $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); + $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); + + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); + wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'orderby' => 'ID', + 'order' => 'ASC', + 'tax_query' => array( + array( + 'taxonomy' => 'wptests_tax2', + 'operator' => 'EXISTS', + ), + ), + ) ); + + $this->assertEqualSets( array( $p2 ), $q->posts ); + } + + /** + * @ticket 29181 + */ + public function test_tax_query_operator_exists_should_ignore_terms() { + register_taxonomy( 'wptests_tax1', 'post' ); + register_taxonomy( 'wptests_tax2', 'post' ); + + $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); + $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); + + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); + wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'orderby' => 'ID', + 'order' => 'ASC', + 'tax_query' => array( + array( + 'taxonomy' => 'wptests_tax2', + 'operator' => 'EXISTS', + 'terms' => array( 'foo', 'bar' ), + ), + ), + ) ); + + $this->assertEqualSets( array( $p2 ), $q->posts ); + } + + /** + * @ticket 29181 + */ + public function test_tax_query_operator_exists_with_no_taxonomy() { + register_taxonomy( 'wptests_tax1', 'post' ); + register_taxonomy( 'wptests_tax2', 'post' ); + + $t1 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax1' ) ); + $t2 = $this->factory->term->create( array( 'taxonomy' => 'wptests_tax2' ) ); + + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' ); + wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'orderby' => 'ID', + 'order' => 'ASC', + 'tax_query' => array( + array( + 'operator' => 'EXISTS', + ), + ), + ) ); + + $this->assertEmpty( $q->posts ); + } + + public function test_tax_query_multiple_queries_relation_and() { + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'bar', + 'name' => 'Bar', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, $t1, 'category' ); + wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + 'relation' => 'AND', + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo' ), + 'field' => 'slug', + ), + array( + 'taxonomy' => 'category', + 'terms' => array( 'bar' ), + 'field' => 'slug', + ), + ), + ) ); + + $this->assertEquals( array( $p2 ), $q->posts ); + } + + public function test_tax_query_multiple_queries_relation_or() { + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'bar', + 'name' => 'Bar', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, $t1, 'category' ); + wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + 'relation' => 'OR', + array( + 'taxonomy' => 'category', + 'terms' => array( 'foo' ), + 'field' => 'slug', + ), + array( + 'taxonomy' => 'category', + 'terms' => array( 'bar' ), + 'field' => 'slug', + ), + ), + ) ); + + $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); + } + + public function test_tax_query_multiple_queries_different_taxonomies() { + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'post_tag', + 'slug' => 'foo', + 'name' => 'Foo', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'bar', + 'name' => 'Bar', + ) ); + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, $t1, 'post_tag' ); + wp_set_object_terms( $p2, $t2, 'category' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + 'relation' => 'OR', + array( + 'taxonomy' => 'post_tag', + 'terms' => array( 'foo' ), + 'field' => 'slug', + ), + array( + 'taxonomy' => 'category', + 'terms' => array( 'bar' ), + 'field' => 'slug', + ), + ), + ) ); + + $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); + } + + /** + * @ticket 29738 + */ + public function test_tax_query_two_nested_queries() { + register_taxonomy( 'foo', 'post' ); + register_taxonomy( 'bar', 'post' ); + + $foo_term_1 = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $foo_term_2 = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $bar_term_1 = $this->factory->term->create( array( + 'taxonomy' => 'bar', + ) ); + $bar_term_2 = $this->factory->term->create( array( + 'taxonomy' => 'bar', + ) ); + + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); + wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); + wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); + wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); + wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); + wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + 'relation' => 'OR', + array( + 'relation' => 'AND', + array( + 'taxonomy' => 'foo', + 'terms' => array( $foo_term_1 ), + 'field' => 'term_id', + ), + array( + 'taxonomy' => 'bar', + 'terms' => array( $bar_term_1 ), + 'field' => 'term_id', ), ), array( - 'taxonomy' => 'testtax2', - 'field' => 'slug', - 'terms' => array( - 'testtax2-slug', + 'relation' => 'AND', + array( + 'taxonomy' => 'foo', + 'terms' => array( $foo_term_2 ), + 'field' => 'term_id', + ), + array( + 'taxonomy' => 'bar', + 'terms' => array( $bar_term_2 ), + 'field' => 'term_id', ), ), + ), + ) ); + + _unregister_taxonomy( 'foo' ); + _unregister_taxonomy( 'bar' ); + + $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); + } + + /** + * @ticket 29738 + */ + public function test_tax_query_one_nested_query_one_first_order_query() { + register_taxonomy( 'foo', 'post' ); + register_taxonomy( 'bar', 'post' ); + + $foo_term_1 = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $foo_term_2 = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $bar_term_1 = $this->factory->term->create( array( + 'taxonomy' => 'bar', + ) ); + $bar_term_2 = $this->factory->term->create( array( + 'taxonomy' => 'bar', + ) ); + + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); + wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); + wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); + wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); + wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); + wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + 'relation' => 'OR', + array( + 'taxonomy' => 'foo', + 'terms' => array( $foo_term_2 ), + 'field' => 'term_id', + ), + array( + 'relation' => 'AND', + array( + 'taxonomy' => 'foo', + 'terms' => array( $foo_term_1 ), + 'field' => 'term_id', + ), + array( + 'taxonomy' => 'bar', + 'terms' => array( $bar_term_1 ), + 'field' => 'term_id', + ), + ), + ), + ) ); + + _unregister_taxonomy( 'foo' ); + _unregister_taxonomy( 'bar' ); + + $this->assertEqualSets( array( $p1, $p2 ), $q->posts ); + } + + /** + * @ticket 29738 + */ + public function test_tax_query_one_double_nested_query_one_first_order_query() { + register_taxonomy( 'foo', 'post' ); + register_taxonomy( 'bar', 'post' ); + + $foo_term_1 = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $foo_term_2 = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $bar_term_1 = $this->factory->term->create( array( + 'taxonomy' => 'bar', + ) ); + $bar_term_2 = $this->factory->term->create( array( + 'taxonomy' => 'bar', + ) ); + + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + $p4 = $this->factory->post->create(); + + wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' ); + wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' ); + wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' ); + wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' ); + wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' ); + wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' ); + + $q = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + 'relation' => 'OR', + array( + 'taxonomy' => 'foo', + 'terms' => array( $foo_term_2 ), + 'field' => 'term_id', + ), + array( + 'relation' => 'AND', + array( + 'taxonomy' => 'foo', + 'terms' => array( $foo_term_1 ), + 'field' => 'term_id', + ), + array( + 'relation' => 'OR', + array( + 'taxonomy' => 'bar', + 'terms' => array( $bar_term_1 ), + 'field' => 'term_id', + ), + array( + 'taxonomy' => 'bar', + 'terms' => array( $bar_term_2 ), + 'field' => 'term_id', + ), + ), + ), + ), + ) ); + + _unregister_taxonomy( 'foo' ); + _unregister_taxonomy( 'bar' ); + + $this->assertEqualSets( array( $p1, $p2, $p3 ), $q->posts ); + } + + /** + * @ticket 20604 + */ + public function test_tax_query_relation_or_both_clauses_empty_terms() { + // An empty tax query should return an empty array, not all posts. + + $this->factory->post->create_many( 10 ); + + $query = new WP_Query( array( + 'fields' => 'ids', + 'update_post_term_cache' => false, + 'update_post_meta_cache' => false, + 'tax_query' => array( + 'relation' => 'OR', + array( + 'taxonomy' => 'post_tag', + 'field' => 'id', + 'terms' => false, + 'operator' => 'IN' + ), + array( + 'taxonomy' => 'category', + 'field' => 'id', + 'terms' => false, + 'operator' => 'IN' + ), ) - ); + ) ); - $q = new WP_Query( $args ); - $object = $q->get_queried_object(); + $posts = $query->get_posts(); + $this->assertEquals( 0 , count( $posts ) ); + } - $expected = get_term( $this->tax_id, 'testtax' ); + /** + * @ticket 20604 + */ + public function test_tax_query_relation_or_one_clause_empty_terms() { + // An empty tax query should return an empty array, not all posts. - // Only compare term_id because object_id may or may not be part of either value. - $this->assertEquals( $expected->term_id, $object->term_id ); + $this->factory->post->create_many( 10 ); + + $query = new WP_Query( array( + 'fields' => 'ids', + 'update_post_term_cache' => false, + 'update_post_meta_cache' => false, + 'tax_query' => array( + 'relation' => 'OR', + array( + 'taxonomy' => 'post_tag', + 'field' => 'id', + 'terms' => array( 'foo' ), + 'operator' => 'IN' + ), + array( + 'taxonomy' => 'category', + 'field' => 'id', + 'terms' => false, + 'operator' => 'IN' + ), + ) + ) ); + + $posts = $query->get_posts(); + $this->assertEquals( 0 , count( $posts ) ); + } + + public function test_tax_query_include_children() { + $cat_a = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Australia' ) ); + $cat_b = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'Sydney', 'parent' => $cat_a ) ); + $cat_c = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'East Syndney', 'parent' => $cat_b ) ); + $cat_d = $this->factory->term->create( array( 'taxonomy' => 'category', 'name' => 'West Syndney', 'parent' => $cat_b ) ); + + $post_a = $this->factory->post->create( array( 'post_category' => array( $cat_a ) ) ); + $post_b = $this->factory->post->create( array( 'post_category' => array( $cat_b ) ) ); + $post_c = $this->factory->post->create( array( 'post_category' => array( $cat_c ) ) ); + $post_d = $this->factory->post->create( array( 'post_category' => array( $cat_d ) ) ); + + $posts = get_posts( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'field' => 'id', + 'terms' => array( $cat_a ), + ) + ) + ) ); + + $this->assertEquals( 4 , count( $posts ) ); + + $posts = get_posts( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'field' => 'id', + 'terms' => array( $cat_a ), + 'include_children' => false + ) + ) + ) ); + + $this->assertEquals( 1 , count( $posts ) ); + + $posts = get_posts( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'field' => 'id', + 'terms' => array( $cat_b ), + ) + ) + ) ); + + $this->assertEquals( 3 , count( $posts ) ); + + $posts = get_posts( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'field' => 'id', + 'terms' => array( $cat_b ), + 'include_children' => false + ) + ) + ) ); + + $this->assertEquals( 1 , count( $posts ) ); + + $posts = get_posts( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'field' => 'id', + 'terms' => array( $cat_c ), + ) + ) + ) ); + + $this->assertEquals( 1 , count( $posts ) ); + + $posts = get_posts( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'field' => 'id', + 'terms' => array( $cat_c ), + 'include_children' => false + ) + ) + ) ); + + $this->assertEquals( 1 , count( $posts ) ); + } + + public function test_tax_query_taxonomy_with_attachments() { + $q = new WP_Query(); + + register_taxonomy_for_object_type( 'post_tag', 'attachment:image' ); + $tag_id = $this->factory->term->create( array( 'slug' => rand_str(), 'name' => rand_str() ) ); + $image_id = $this->factory->attachment->create_object( 'image.jpg', 0, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + wp_set_object_terms( $image_id, $tag_id, 'post_tag' ); + + $posts = $q->query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'post_type' => 'attachment', + 'post_status' => 'inherit', + 'tax_query' => array( + array( + 'taxonomy' => 'post_tag', + 'field' => 'term_id', + 'terms' => array( $tag_id ) + ) + ) + ) ); + + $this->assertEquals( array( $image_id ), $posts ); + } + + public function test_tax_query_no_taxonomy() { + $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) ); + $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) ); + + $response1 = new WP_Query( array( + 'tax_query' => array( + array( 'terms' => array( $cat_id ) ) + ) + ) ); + $this->assertEmpty( $response1->posts ); + + $response2 = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'taxonomy' => 'category', + 'terms' => array( $cat_id ) + ) + ) + ) ); + $this->assertNotEmpty( $response2->posts ); + + $term = get_category( $cat_id ); + $response3 = new WP_Query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'tax_query' => array( + array( + 'field' => 'term_taxonomy_id', + 'terms' => array( $term->term_taxonomy_id ) + ) + ) + ) ); + $this->assertNotEmpty( $response3->posts ); + } + + public function test_term_taxonomy_id_field_no_taxonomy() { + $q = new WP_Query(); + + $posts = $this->factory->post->create_many( 5 ); + + $cats = $tags = array(); + + // need term_taxonomy_ids in addition to term_ids, so no factory + for ( $i = 0; $i < 5; $i++ ) { + $cats[$i] = wp_insert_term( 'category-' . $i , 'category' ); + $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' ); + + // post 0 gets all terms + wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true ); + wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true ); + } + + wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); + wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' ); + + wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' ); + wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); + + wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); + wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); + + $results1 = $q->query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'orderby' => 'ID', + 'order' => 'ASC', + 'tax_query' => array( + 'relation' => 'OR', + array( + 'field' => 'term_taxonomy_id', + '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'] ), + 'operator' => 'AND', + 'include_children' => false, + ), + array( + 'field' => 'term_taxonomy_id', + 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), + 'operator' => 'AND', + 'include_children' => false, + ) + ) + ) ); + + $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' ); + + $results2 = $q->query( array( + 'fields' => 'ids', + 'update_post_meta_cache' => false, + 'update_post_term_cache' => false, + 'orderby' => 'ID', + 'order' => 'ASC', + 'tax_query' => array( + 'relation' => 'AND', + array( + 'field' => 'term_taxonomy_id', + 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ), + 'operator' => 'IN', + 'include_children' => false, + ), + array( + 'field' => 'term_taxonomy_id', + 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), + 'operator' => 'IN', + 'include_children' => false, + ) + ) + ) ); + + $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' ); + } + + /** + * @ticket 29738 + */ + public function test_populate_taxonomy_query_var_from_tax_query() { + register_taxonomy( 'foo', 'post' ); + $t = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $c = $this->factory->term->create( array( + 'taxonomy' => 'category', + ) ); + + $q = new WP_Query( array( + 'tax_query' => array( + // Empty terms mean that this one should be skipped + array( + 'taxonomy' => 'bar', + 'terms' => array(), + ), + + // Category and post tags should be skipped + array( + 'taxonomy' => 'category', + 'terms' => array( $c ), + ), + + array( + 'taxonomy' => 'foo', + 'terms' => array( $t ), + ), + ), + ) ); + + $this->assertSame( 'foo', $q->get( 'taxonomy' ) ); + + _unregister_taxonomy( 'foo' ); + } + + public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() { + register_taxonomy( 'foo', 'post' ); + register_taxonomy( 'foo1', 'post' ); + $t = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + + $q = new WP_Query( array( + 'taxonomy' => 'bar', + 'tax_query' => array( + array( + 'taxonomy' => 'foo', + 'terms' => array( $t ), + ), + ), + ) ); + + $this->assertSame( 'bar', $q->get( 'taxonomy' ) ); + + _unregister_taxonomy( 'foo' ); + _unregister_taxonomy( 'foo1' ); + } + + public function test_populate_term_query_var_from_tax_query() { + register_taxonomy( 'foo', 'post' ); + $t = $this->factory->term->create( array( + 'taxonomy' => 'foo', + 'slug' => 'bar', + ) ); + + $q = new WP_Query( array( + 'tax_query' => array( + array( + 'taxonomy' => 'foo', + 'terms' => array( 'bar' ), + 'field' => 'slug', + ), + ), + ) ); + + $this->assertSame( 'bar', $q->get( 'term' ) ); + + _unregister_taxonomy( 'foo' ); + } + + public function test_populate_term_id_query_var_from_tax_query() { + register_taxonomy( 'foo', 'post' ); + $t = $this->factory->term->create( array( + 'taxonomy' => 'foo', + 'slug' => 'bar', + ) ); + + $q = new WP_Query( array( + 'tax_query' => array( + array( + 'taxonomy' => 'foo', + 'terms' => array( $t ), + 'field' => 'term_id', + ), + ), + ) ); + + $this->assertEquals( $t, $q->get( 'term_id' ) ); + + _unregister_taxonomy( 'foo' ); + } + + /** + * @ticket 29738 + */ + public function test_populate_cat_category_name_query_var_from_tax_query() { + register_taxonomy( 'foo', 'post' ); + $t = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $c = $this->factory->term->create( array( + 'taxonomy' => 'category', + 'slug' => 'bar', + ) ); + + $q = new WP_Query( array( + 'tax_query' => array( + // Non-category should be skipped + array( + 'taxonomy' => 'foo', + 'terms' => array( $t ), + ), + + // Empty terms mean that this one should be skipped + array( + 'taxonomy' => 'category', + 'terms' => array(), + ), + + // Category and post tags should be skipped + array( + 'taxonomy' => 'category', + 'terms' => array( $c ), + ), + ), + ) ); + + $this->assertEquals( $c, $q->get( 'cat' ) ); + $this->assertEquals( 'bar', $q->get( 'category_name' ) ); + + _unregister_taxonomy( 'foo' ); + } + + /** + * @ticket 29738 + */ + public function test_populate_tag_id_query_var_from_tax_query() { + register_taxonomy( 'foo', 'post' ); + $t = $this->factory->term->create( array( + 'taxonomy' => 'foo', + ) ); + $tag = $this->factory->term->create( array( + 'taxonomy' => 'post_tag', + 'slug' => 'bar', + ) ); + + $q = new WP_Query( array( + 'tax_query' => array( + // Non-tag should be skipped + array( + 'taxonomy' => 'foo', + 'terms' => array( $t ), + ), + + // Empty terms mean that this one should be skipped + array( + 'taxonomy' => 'post_tag', + 'terms' => array(), + ), + + // Category and post tags should be skipped + array( + 'taxonomy' => 'post_tag', + 'terms' => array( $tag ), + ), + ), + ) ); + + $this->assertEquals( $tag, $q->get( 'tag_id' ) ); + + _unregister_taxonomy( 'foo' ); } }