Make WordPress Core

Changeset 30848


Ignore:
Timestamp:
12/14/2014 07:30:40 PM (9 years ago)
Author:
johnbillion
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'.

Merges [30846] to the 4.1 branch.

Props barrykooij.
Fixes #30681

Location:
branches/4.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1

  • branches/4.1/src/wp-includes/meta.php

    r30753 r30848  
    14201420                    break;
    14211421
     1422                // 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 );
  • branches/4.1/tests/phpunit/tests/post/query.php

    r29979 r30848  
    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.