Make WordPress Core

Changeset 33724


Ignore:
Timestamp:
08/24/2015 09:21:49 PM (11 years ago)
Author:
boonebgorges
Message:

In WP_Query::parse_tax_query(), allow 'cat' and 'tag' querystrings to be formatted as arrays.

See [33095] #32454 for a previous fix related to custom taxonomies.

Props Veraxus.
Fixes #33532.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r33722 r33724  
    18921892                }
    18931893
     1894                // If querystring 'cat' is an array, implode it.
     1895                if ( is_array( $q['cat'] ) ) {
     1896                        $q['cat'] = implode( ',', $q['cat'] );
     1897                }
     1898
    18941899                // Category stuff
    18951900                if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
     
    19651970                                'include_children' => false
    19661971                        );
     1972                }
     1973
     1974                // If querystring 'tag' is array, implode it.
     1975                if ( is_array( $q['tag'] ) ) {
     1976                        $q['tag'] = implode( ',', $q['tag'] );
    19671977                }
    19681978
  • trunk/tests/phpunit/tests/query.php

    r33102 r33724  
    135135        }
    136136
     137        public function test_cat_querystring_single_term() {
     138                $c1 = $this->factory->category->create( array(
     139                        'name' => 'Test Category 1',
     140                        'slug' => 'test1',
     141                ) );
     142                $c2 = $this->factory->category->create( array(
     143                        'name' => 'Test Category 2',
     144                        'slug' => 'test2',
     145                ) );
     146
     147                $p1 = $this->factory->post->create();
     148                $p2 = $this->factory->post->create();
     149                $p3 = $this->factory->post->create();
     150
     151                wp_set_object_terms( $p1, $c1, 'category' );
     152                wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
     153                wp_set_object_terms( $p3, $c2, 'category' );
     154
     155                $url = add_query_arg( array(
     156                        'cat' => $c1,
     157                ), '/' );
     158
     159                $this->go_to( $url );
     160
     161                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     162
     163                $this->assertEqualSets( array( $p1, $p2 ), $matching_posts );
     164        }
     165
     166        public function test_category_querystring_multiple_terms_comma_separated() {
     167                $c1 = $this->factory->category->create( array(
     168                        'name' => 'Test Category 1',
     169                        'slug' => 'test1',
     170                ) );
     171                $c2 = $this->factory->category->create( array(
     172                        'name' => 'Test Category 2',
     173                        'slug' => 'test2',
     174                ) );
     175                $c3 = $this->factory->category->create( array(
     176                        'name' => 'Test Category 3',
     177                        'slug' => 'test3',
     178                ) );
     179
     180                $p1 = $this->factory->post->create();
     181                $p2 = $this->factory->post->create();
     182                $p3 = $this->factory->post->create();
     183                $p4 = $this->factory->post->create();
     184
     185                wp_set_object_terms( $p1, $c1, 'category' );
     186                wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
     187                wp_set_object_terms( $p3, $c2, 'category' );
     188                wp_set_object_terms( $p4, $c3, 'category' );
     189
     190                $url = add_query_arg( array(
     191                        'cat' => implode( ',',array( $c1,$c2 ) ),
     192                ), '/' );
     193
     194                $this->go_to( $url );
     195
     196                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     197
     198                $this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
     199        }
     200
     201        /**
     202         * @ticket 33532
     203         */
     204        public function test_category_querystring_multiple_terms_formatted_as_array() {
     205                $c1 = $this->factory->category->create( array(
     206                        'name' => 'Test Category 1',
     207                        'slug' => 'test1',
     208                ) );
     209                $c2 = $this->factory->category->create( array(
     210                        'name' => 'Test Category 2',
     211                        'slug' => 'test2',
     212                ) );
     213                $c3 = $this->factory->category->create( array(
     214                        'name' => 'Test Category 3',
     215                        'slug' => 'test3',
     216                ) );
     217
     218                $p1 = $this->factory->post->create();
     219                $p2 = $this->factory->post->create();
     220                $p3 = $this->factory->post->create();
     221                $p4 = $this->factory->post->create();
     222
     223                wp_set_object_terms( $p1, $c1, 'category' );
     224                wp_set_object_terms( $p2, array( $c1, $c2 ), 'category' );
     225                wp_set_object_terms( $p3, $c2, 'category' );
     226                wp_set_object_terms( $p4, $c3, 'category' );
     227
     228                $url = add_query_arg( array(
     229                        'cat' => array( $c1, $c2 ),
     230                ), '/' );
     231
     232                $this->go_to( $url );
     233
     234                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     235
     236                $this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
     237        }
     238
     239
     240        public function test_tag_querystring_single_term() {
     241                $t1 = $this->factory->tag->create_and_get( array(
     242                        'name' => 'Test Tag 1',
     243                        'slug' => 'test1',
     244                ) );
     245                $t2 = $this->factory->tag->create_and_get( array(
     246                        'name' => 'Test Tag 2',
     247                        'slug' => 'test2',
     248                ) );
     249
     250                $p1 = $this->factory->post->create();
     251                $p2 = $this->factory->post->create();
     252                $p3 = $this->factory->post->create();
     253
     254                wp_set_object_terms( $p1, $t1->slug, 'post_tag' );
     255                wp_set_object_terms( $p2, array( $t1->slug, $t2->slug ), 'post_tag' );
     256                wp_set_object_terms( $p3, $t2->slug, 'post_tag' );
     257
     258                $url = add_query_arg( array(
     259                        'tag' => $t1,
     260                ), '/' );
     261
     262                $this->go_to( $url );
     263
     264                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     265
     266                $this->assertEqualSets( array( $p1, $p2 ), $matching_posts );
     267        }
     268
     269        public function test_tag_querystring_multiple_terms_comma_separated() {
     270                $c1 = $this->factory->tag->create_and_get( array(
     271                        'name' => 'Test Tag 1',
     272                        'slug' => 'test1',
     273                ) );
     274                $c2 = $this->factory->tag->create_and_get( array(
     275                        'name' => 'Test Tag 2',
     276                        'slug' => 'test2',
     277                ) );
     278                $c3 = $this->factory->tag->create_and_get( array(
     279                        'name' => 'Test Tag 3',
     280                        'slug' => 'test3',
     281                ) );
     282
     283                $p1 = $this->factory->post->create();
     284                $p2 = $this->factory->post->create();
     285                $p3 = $this->factory->post->create();
     286                $p4 = $this->factory->post->create();
     287
     288                wp_set_object_terms( $p1, $c1->slug, 'post_tag' );
     289                wp_set_object_terms( $p2, array( $c1->slug, $c2->slug ), 'post_tag' );
     290                wp_set_object_terms( $p3, $c2->slug, 'post_tag' );
     291                wp_set_object_terms( $p4, $c3->slug, 'post_tag' );
     292
     293                $url = add_query_arg( array(
     294                        'tag' => implode( ',',array( $c1->slug,$c2->slug ) ),
     295                ), '/' );
     296
     297                $this->go_to( $url );
     298
     299                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     300
     301                $this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
     302        }
     303
     304        /**
     305         * @ticket #33532
     306         */
     307        public function test_tag_querystring_multiple_terms_formatted_as_array() {
     308                $c1 = $this->factory->tag->create_and_get( array(
     309                        'name' => 'Test Tag 1',
     310                        'slug' => 'test1',
     311                ) );
     312                $c2 = $this->factory->tag->create_and_get( array(
     313                        'name' => 'Test Tag 2',
     314                        'slug' => 'test2',
     315                ) );
     316                $c3 = $this->factory->tag->create_and_get( array(
     317                        'name' => 'Test Tag 3',
     318                        'slug' => 'test3',
     319                ) );
     320
     321                $p1 = $this->factory->post->create();
     322                $p2 = $this->factory->post->create();
     323                $p3 = $this->factory->post->create();
     324                $p4 = $this->factory->post->create();
     325
     326                wp_set_object_terms( $p1, $c1->slug, 'post_tag' );
     327                wp_set_object_terms( $p2, array( $c1->slug, $c2->slug ), 'post_tag' );
     328                wp_set_object_terms( $p3, $c2->slug, 'post_tag' );
     329                wp_set_object_terms( $p4, $c3->slug, 'post_tag' );
     330
     331                $url = add_query_arg( array(
     332                        'tag' => array($c1->slug,$c2->slug),
     333                ), '/' );
     334
     335                $this->go_to( $url );
     336
     337                $matching_posts = wp_list_pluck( $GLOBALS['wp_query']->posts, 'ID' );
     338
     339                $this->assertEqualSets( array( $p1, $p2, $p3 ), $matching_posts );
     340        }
     341
    137342        public function test_custom_taxonomy_querystring_single_term() {
    138343                register_taxonomy( 'test_tax_cat', 'post' );
Note: See TracChangeset for help on using the changeset viewer.