Make WordPress Core


Ignore:
Timestamp:
12/14/2014 07:00:31 PM (12 years ago)
Author:
boonebgorges
Message:

In WP_Meta_Query, interpret 'value' correctly when used with EXISTS/NOT EXISTS.

As in earlier versions, EXISTS with a value is equivalent to '=', while NOT
EXISTS should always ignore 'value'.

Props barrykooij.
Fixes #30681 for trunk.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/query.php

    r29979 r30846  
    539539
    540540        /**
     541         * @ticket 30681
     542         */
     543        public function test_meta_query_compare_exists() {
     544                $posts = $this->factory->post->create_many( 3 );
     545                add_post_meta( $posts[0], 'foo', 'bar' );
     546                add_post_meta( $posts[2], 'foo', 'baz' );
     547
     548                $query = new WP_Query( array(
     549                        'fields' => 'ids',
     550                        'meta_query' => array(
     551                                array(
     552                                        'compare' => 'EXISTS',
     553                                        'key' => 'foo',
     554                                ),
     555                        ),
     556                ) );
     557
     558                $this->assertEqualSets( array( $posts[0], $posts[2] ), $query->posts );
     559        }
     560
     561        /**
     562         * @ticket 30681
     563         */
     564        public function test_meta_query_compare_exists_with_value_should_convert_to_equals() {
     565                $posts = $this->factory->post->create_many( 3 );
     566                add_post_meta( $posts[0], 'foo', 'bar' );
     567                add_post_meta( $posts[2], 'foo', 'baz' );
     568
     569                $query = new WP_Query( array(
     570                        'fields' => 'ids',
     571                        'meta_query' => array(
     572                                array(
     573                                        'compare' => 'EXISTS',
     574                                        'value' => 'baz',
     575                                        'key' => 'foo',
     576                                ),
     577                        ),
     578                ) );
     579
     580                $this->assertEqualSets( array( $posts[2] ), $query->posts );
     581        }
     582
     583        /**
     584         * @ticket 30681
     585         */
     586        public function test_meta_query_compare_not_exists_should_ignore_value() {
     587                $posts = $this->factory->post->create_many( 3 );
     588                add_post_meta( $posts[0], 'foo', 'bar' );
     589                add_post_meta( $posts[2], 'foo', 'baz' );
     590
     591                $query = new WP_Query( array(
     592                        'fields' => 'ids',
     593                        'meta_query' => array(
     594                                array(
     595                                        'compare' => 'NOT EXISTS',
     596                                        'value' => 'bar',
     597                                        'key' => 'foo',
     598                                ),
     599                        ),
     600                ) );
     601
     602                $this->assertEqualSets( array( $posts[1] ), $query->posts );
     603        }
     604
     605        /**
    541606         * @ticket 18158
    542607         * @group meta
Note: See TracChangeset for help on using the changeset viewer.