public
Last active

WP_Query `NOT EXISTS` examples

  • Download Gist
23268.diff
Diff
1 2 3 4 5 6 7 8 9 10 11 12 13
diff --git wp-includes/meta.php wp-includes/meta.php
index 8a9dad9..d3e4f82 100644
--- wp-includes/meta.php
+++ wp-includes/meta.php
@@ -708,7 +708,7 @@ class WP_Meta_Query {
// Split out the meta_key only queries (we can only do this for OR)
if ( 'OR' == $this->relation ) {
foreach ( $this->queries as $k => $q ) {
- if ( ! isset( $q['value'] ) && ! empty( $q['key'] ) )
+ if ( ( empty( $q['compare'] ) || 'NOT EXISTS' != $q['compare'] ) && ! isset( $q['value'] ) && ! empty( $q['key'] ) )
$key_only_queries[$k] = $q;
else
$queries[$k] = $q;
wp_query_not_exists.sql
SQL
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
/*
 
Generated with:
 
$q = new \WP_Query(array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'a_key',
'compare' => 'NOT EXISTS',
),
)
));
 
*/
 
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id
WHERE 1=1
AND wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish' OR
wp_posts.post_status = 'private'
)
AND (
wp_postmeta.meta_key = 'a_key' -- Not expected
)
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10;
 
/*
 
Generated with:
 
$q = new \WP_Query(array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'a_key',
'compare' => 'NOT EXISTS',
),
)
));
 
*/
 
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'a_key')
WHERE 1=1
AND wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish' OR
wp_posts.post_status = 'private'
)
AND ( wp_postmeta.post_id IS NULL ) -- expected!
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10;
 
/*
 
Specifying a value with the OR query also makes NOT EXISTS work correctly.
 
$q = new \WP_Query(array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'a_key',
'compare' => 'NOT EXISTS',
'value' => 'n/a',
),
)
));
 
*/
 
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'a_key')
WHERE 1=1
AND wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'
)
AND ( wp_postmeta.post_id IS NULL ) -- expected!
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10;
 
/*
 
With the patched meta query builder:
 
 
$q = new \WP_Query(array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'a_key',
'compare' => 'NOT EXISTS',
),
)
));
 
*/
 
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
LEFT JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id AND wp_postmeta.meta_key = 'a_key')
WHERE 1=1
AND wp_posts.post_type = 'post'
AND (
wp_posts.post_status = 'publish'
OR wp_posts.post_status = 'private'
)
AND ( wp_postmeta.post_id IS NULL ) -- expected
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 10;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.