mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-06 01:10:04 +00:00
Allow LIKE queries against the 'key' value in meta queries.
The new `compare_key=LIKE` parameter works in conjunction with `key` in a similar way to the `compare=LIKE` and `value`: by doing a "compares" `LIKE` query. This allows developers to do partial matches against keys when doing meta queries. Props mariovalney, chasewg. Fixes #42409. git-svn-id: https://develop.svn.wordpress.org/trunk@42768 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -1848,4 +1848,78 @@ class Tests_Query_MetaQuery extends WP_UnitTestCase {
|
||||
|
||||
$this->assertEqualSets( array( 'foo_key', 'foo_key-1', 'foo_key-2' ), array_keys( $q->meta_query->get_clauses() ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 42409
|
||||
*/
|
||||
public function test_compare_key_like() {
|
||||
$posts = self::factory()->post->create_many( 3 );
|
||||
|
||||
add_post_meta( $posts[0], 'aaa_foo_aaa', 'abc' );
|
||||
add_post_meta( $posts[1], 'aaa_bar_aaa', 'abc' );
|
||||
add_post_meta( $posts[2], 'aaa_foo_bbb', 'abc' );
|
||||
|
||||
$q = new WP_Query(
|
||||
array(
|
||||
'meta_query' => array(
|
||||
array(
|
||||
'compare_key' => 'LIKE',
|
||||
'key' => 'aa_foo',
|
||||
),
|
||||
),
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEqualSets( array( $posts[0], $posts[2] ), $q->posts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 42409
|
||||
*/
|
||||
public function test_meta_compare_key_like() {
|
||||
$posts = self::factory()->post->create_many( 3 );
|
||||
|
||||
add_post_meta( $posts[0], 'aaa_foo_aaa', 'abc' );
|
||||
add_post_meta( $posts[1], 'aaa_bar_aaa', 'abc' );
|
||||
add_post_meta( $posts[2], 'aaa_foo_bbb', 'abc' );
|
||||
|
||||
$q = new WP_Query(
|
||||
array(
|
||||
'meta_compare_key' => 'LIKE',
|
||||
'meta_key' => 'aa_foo',
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEqualSets( array( $posts[0], $posts[2] ), $q->posts );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 42409
|
||||
*/
|
||||
public function test_compare_key_like_with_not_exists_compare() {
|
||||
$posts = self::factory()->post->create_many( 3 );
|
||||
|
||||
add_post_meta( $posts[0], 'aaa_foo_aaa', 'abc' );
|
||||
add_post_meta( $posts[1], 'aaa_bar_aaa', 'abc' );
|
||||
add_post_meta( $posts[2], 'bar', 'abc' );
|
||||
|
||||
$q = new WP_Query(
|
||||
array(
|
||||
'meta_query' => array(
|
||||
'relation' => 'AND',
|
||||
array(
|
||||
'compare_key' => 'LIKE',
|
||||
'key' => 'bar',
|
||||
'compare' => 'NOT EXISTS',
|
||||
),
|
||||
),
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEqualSets( array( $posts[0] ), $q->posts );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user