Skip to content

Commit c735515

Browse files
authored
Merge pull request #232 from humanmade/fix-meta-query-sniff
Support 'relation' element in meta query sniff.
2 parents 7791747 + cdf723e commit c735515

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

HM/Sniffs/Performance/SlowMetaQuerySniff.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,16 @@ protected function check_meta_query_item( int $array_open ) {
136136
return;
137137
}
138138

139-
// Otherwise, recurse.
140139
foreach ( $elements as $element ) {
140+
if ( isset( $element['index_start'] ) ) {
141+
$index = $this->strip_quotes( $this->tokens[ $element['index_start'] ]['content'] );
142+
if ( strtolower( $index ) === 'relation' ) {
143+
// Skip 'relation' element.
144+
continue;
145+
}
146+
}
147+
148+
// Otherwise, recurse.
141149
$this->check_meta_query_item( $element['value_start'] );
142150
}
143151
}

tests/fixtures/pass/meta-queries.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,44 @@
1919
],
2020
] );
2121

22+
// Specifying relation is OK.
23+
new WP_Query( [
24+
'meta_query' => [
25+
'relation' => 'AND',
26+
[
27+
'key' => 'foo',
28+
'compare' => 'EXISTS',
29+
],
30+
[
31+
'key' => 'bar',
32+
'compare' => 'NOT EXISTS',
33+
],
34+
],
35+
] );
36+
new WP_Query( [
37+
'meta_query' => [
38+
'relation' => 'OR',
39+
[
40+
'key' => 'foo',
41+
'compare' => 'NOT EXISTS',
42+
],
43+
[
44+
'key' => 'bar',
45+
'compare' => 'EXISTS',
46+
],
47+
],
48+
] );
49+
$relation = 'OR';
50+
new WP_Query( [
51+
'meta_query' => [
52+
'relation' => $relation,
53+
[
54+
'key' => 'foo',
55+
'compare' => 'EXISTS',
56+
],
57+
],
58+
] );
59+
2260
// Ignores should work.
2361
new WP_Query( [
2462
'meta_query' => [

0 commit comments

Comments
 (0)