From 53021ef6fef089c2931b6d5ec1b5bb773837a6e6 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Mon, 6 Jul 2015 20:36:18 +0000 Subject: [PATCH] In `WP_Query::parse_tax_query()`, allow taxonomy querystring to be formatted as an array. Props Veraxus. Fixes #32454. git-svn-id: https://develop.svn.wordpress.org/trunk@33095 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 4 ++ tests/phpunit/tests/query.php | 79 +++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 30ca641afc..7cbac9db88 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -1867,6 +1867,10 @@ class WP_Query { $term = $q[$t->query_var]; + if ( is_array( $term ) ) { + $term = implode( ',', $term ); + } + if ( strpos($term, '+') !== false ) { $terms = preg_split( '/[+]+/', $term ); foreach ( $terms as $term ) { diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index f12dccc41d..ab1e61f42a 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -133,4 +133,83 @@ class Tests_Query extends WP_UnitTestCase { $this->assertContains( "ORDER BY $wpdb->posts.post_title DESC, $wpdb->posts.post_date DESC", $q->request ); } + + public function test_custom_taxonomy_querystring_single_term() { + register_taxonomy( 'test_tax_cat', 'post' ); + + wp_insert_term( 'test1', 'test_tax_cat' ); + wp_insert_term( 'test2', 'test_tax_cat' ); + wp_insert_term( 'test3', 'test_tax_cat' ); + + $p1 = $this->factory->post->create(); + $p2 = $this->factory->post->create(); + $p3 = $this->factory->post->create(); + + wp_set_object_terms( $p1, 'test1', 'test_tax_cat' ); + wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' ); + wp_set_object_terms( $p3, 'test2', 'test_tax_cat' ); + + $url = add_query_arg( array( + 'test_tax_cat' => 'test1', + ), '/' ); + + $this->go_to( $url ); + + $this->assertEquals( array( $p1, $p2 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) ); + } + + public function test_custom_taxonomy_querystring_multiple_terms_comma_separated() { + register_taxonomy( 'test_tax_cat', 'post' ); + + wp_insert_term( 'test1', 'test_tax_cat' ); + wp_insert_term( 'test2', 'test_tax_cat' ); + wp_insert_term( 'test3', 'test_tax_cat' ); + + $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, 'test1', 'test_tax_cat' ); + wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' ); + wp_set_object_terms( $p3, 'test2', 'test_tax_cat' ); + wp_set_object_terms( $p4, "test3", 'test_tax_cat' ); + + $url = add_query_arg( array( + 'test_tax_cat' => 'test1,test2', + ), '/' ); + + $this->go_to( $url ); + + $this->assertEquals( array( $p1, $p2, $p3 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) ); + } + + /** + * @ticket 32454 + */ + public function test_custom_taxonomy_querystring_multiple_terms_formatted_as_array() { + register_taxonomy( 'test_tax_cat', 'post' ); + + wp_insert_term( 'test1', 'test_tax_cat' ); + wp_insert_term( 'test2', 'test_tax_cat' ); + wp_insert_term( 'test3', 'test_tax_cat' ); + + $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, 'test1', 'test_tax_cat' ); + wp_set_object_terms( $p2, array( 'test1', 'test2' ), 'test_tax_cat' ); + wp_set_object_terms( $p3, 'test2', 'test_tax_cat' ); + wp_set_object_terms( $p4, "test3", 'test_tax_cat' ); + + $url = add_query_arg( array( + 'test_tax_cat' => array( 'test1', 'test2' ), + ), '/' ); + + $this->go_to( $url ); + + $this->assertEquals( array( $p1, $p2, $p3 ), wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' ) ); + } }