Make WordPress Core

Ticket #30681: 30681.diff

File 30681.diff, 2.4 KB (added by boonebgorges, 11 years ago)
  • src/wp-includes/meta.php

    diff --git src/wp-includes/meta.php src/wp-includes/meta.php
    index 44fb7d2..8dc090c 100644
    class WP_Meta_Query { 
    14191419                                        $where = $wpdb->prepare( '%s', $meta_value );
    14201420                                        break;
    14211421
     1422                                // Legacy: EXISTS with a value is interpreted as '='.
     1423                                case 'EXISTS' :
     1424                                        $meta_compare = '=';
     1425                                        $where = $wpdb->prepare( '%s', $meta_value );
     1426                                        break;
     1427
     1428                                // 'value' is ignored for NOT EXISTS.
     1429                                case 'NOT EXISTS' :
     1430                                        $where = '';
     1431                                        break;
     1432
    14221433                                default :
    14231434                                        $where = $wpdb->prepare( '%s', $meta_value );
    14241435                                        break;
  • tests/phpunit/tests/post/query.php

    diff --git tests/phpunit/tests/post/query.php tests/phpunit/tests/post/query.php
    index 723d061..722cad7 100644
    class Tests_Post_Query extends WP_UnitTestCase { 
    538538        }
    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
    543608         */