Make WordPress Core

Changeset 27689


Ignore:
Timestamp:
03/24/2014 07:56:54 PM (12 years ago)
Author:
wonderboymusic
Message:

When using meta_query in a WP_Query, passing NOT EXISTS or '' to compare should not require value to be set. The resulting SQL should then produce the appropriate OR clause for existence of non-existence after passing the query to the $key_only_queries stack internally.

Adds unit tests.

Props chrisguitarguy, for the original patch.
Fixes #23268.

Location:
trunk
Files:
2 edited

Legend:

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

    r27562 r27689  
    754754                if ( 'OR' == $this->relation ) {
    755755                        foreach ( $this->queries as $k => $q ) {
    756                                 if ( ! array_key_exists( 'value', $q ) && ! empty( $q['key'] ) )
     756                                if ( ( empty( $q['compare'] ) || 'NOT EXISTS' != $q['compare'] ) && ! array_key_exists( 'value', $q ) && ! empty( $q['key'] ) )
    757757                                        $key_only_queries[$k] = $q;
    758758                                else
  • trunk/tests/phpunit/tests/meta/query.php

    r26055 r27689  
    153153                $this->assertEquals( 'CHAR', $query->get_cast_for_type( 'ANYTHING ELSE' ) );
    154154        }
     155
     156        function test_not_exists() {
     157                global $wpdb;
     158
     159                $query = new WP_Meta_Query( array(
     160                        'relation' => 'OR',
     161                        array(
     162                                'key' => 'exclude',
     163                                'compare' => 'NOT EXISTS'
     164                        ),
     165                        array(
     166                                'key' => 'exclude',
     167                                'compare' => '!=',
     168                                'value' => '1'
     169                        ),
     170                ) );
     171
     172                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     173                $this->assertNotContains( "{$wpdb->postmeta}.meta_key = 'exclude'\nOR", $sql['where'] );
     174                $this->assertContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
     175        }
     176
     177        function test_empty_compare() {
     178                global $wpdb;
     179
     180                $query = new WP_Meta_Query( array(
     181                        'relation' => 'OR',
     182                        array(
     183                                'key' => 'exclude',
     184                                'compare' => ''
     185                        ),
     186                        array(
     187                                'key' => 'exclude',
     188                                'compare' => '!=',
     189                                'value' => '1'
     190                        ),
     191                ) );
     192
     193                $sql = $query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     194                $this->assertContains( "{$wpdb->postmeta}.meta_key = 'exclude'\nOR", $sql['where'] );
     195                $this->assertNotContains( "{$wpdb->postmeta}.post_id IS NULL", $sql['where'] );
     196        }
    155197}
Note: See TracChangeset for help on using the changeset viewer.