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. git-svn-id: https://develop.svn.wordpress.org/trunk@30846 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
91ac188a12
commit
093de0f0be
@ -1419,6 +1419,17 @@ class WP_Meta_Query {
|
||||
$where = $wpdb->prepare( '%s', $meta_value );
|
||||
break;
|
||||
|
||||
// EXISTS with a value is interpreted as '='.
|
||||
case 'EXISTS' :
|
||||
$meta_compare = '=';
|
||||
$where = $wpdb->prepare( '%s', $meta_value );
|
||||
break;
|
||||
|
||||
// 'value' is ignored for NOT EXISTS.
|
||||
case 'NOT EXISTS' :
|
||||
$where = '';
|
||||
break;
|
||||
|
||||
default :
|
||||
$where = $wpdb->prepare( '%s', $meta_value );
|
||||
break;
|
||||
|
@ -537,6 +537,71 @@ class Tests_Post_Query extends WP_UnitTestCase {
|
||||
$this->assertEqualSets( $expected, $query->posts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30681
|
||||
*/
|
||||
public function test_meta_query_compare_exists() {
|
||||
$posts = $this->factory->post->create_many( 3 );
|
||||
add_post_meta( $posts[0], 'foo', 'bar' );
|
||||
add_post_meta( $posts[2], 'foo', 'baz' );
|
||||
|
||||
$query = new WP_Query( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'compare' => 'EXISTS',
|
||||
'key' => 'foo',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( $posts[0], $posts[2] ), $query->posts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30681
|
||||
*/
|
||||
public function test_meta_query_compare_exists_with_value_should_convert_to_equals() {
|
||||
$posts = $this->factory->post->create_many( 3 );
|
||||
add_post_meta( $posts[0], 'foo', 'bar' );
|
||||
add_post_meta( $posts[2], 'foo', 'baz' );
|
||||
|
||||
$query = new WP_Query( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'compare' => 'EXISTS',
|
||||
'value' => 'baz',
|
||||
'key' => 'foo',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( $posts[2] ), $query->posts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30681
|
||||
*/
|
||||
public function test_meta_query_compare_not_exists_should_ignore_value() {
|
||||
$posts = $this->factory->post->create_many( 3 );
|
||||
add_post_meta( $posts[0], 'foo', 'bar' );
|
||||
add_post_meta( $posts[2], 'foo', 'baz' );
|
||||
|
||||
$query = new WP_Query( array(
|
||||
'fields' => 'ids',
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'compare' => 'NOT EXISTS',
|
||||
'value' => 'bar',
|
||||
'key' => 'foo',
|
||||
),
|
||||
),
|
||||
) );
|
||||
|
||||
$this->assertEqualSets( array( $posts[1] ), $query->posts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 18158
|
||||
* @group meta
|
||||
|
Loading…
Reference in New Issue
Block a user