From aebd57c588765aa71cd52c018529b0c9f02c3f31 Mon Sep 17 00:00:00 2001 From: nacin Date: Fri, 29 Jun 2012 19:59:29 +0000 Subject: [PATCH] Add NOT EXISTS to meta queries, allowing you to query for the non-existence of a meta key. You could already use EXISTS by omitting a value to check. props georgestephanis, scribu fixes #18158 git-svn-id: http://core.svn.wordpress.org/trunk@21185 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/meta.php | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/wp-includes/meta.php b/wp-includes/meta.php index d6984f6200..39646440f0 100644 --- a/wp-includes/meta.php +++ b/wp-includes/meta.php @@ -720,10 +720,35 @@ class WP_Meta_Query { elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) $meta_type = 'CHAR'; + $meta_value = isset( $q['value'] ) ? $q['value'] : null; + + if ( isset( $q['compare'] ) ) + $meta_compare = strtoupper( $q['compare'] ); + else + $meta_compare = is_array( $meta_value ) ? 'IN' : '='; + + if ( ! in_array( $meta_compare, array( + '=', '!=', '>', '>=', '<', '<=', + 'LIKE', 'NOT LIKE', + 'IN', 'NOT IN', + 'BETWEEN', 'NOT BETWEEN', + 'NOT EXISTS' + ) ) ) + $meta_compare = '='; + $i = count( $join ); $alias = $i ? 'mt' . $i : $meta_table; - // Set JOIN + if ( 'NOT EXISTS' == $meta_compare ) { + $join[$i] = "LEFT JOIN $meta_table"; + $join[$i] .= $i ? " AS $alias" : ''; + $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column AND $alias.meta_key = '$meta_key')"; + + $where[$k] = ' ' . $alias . '.' . $meta_id_column . ' IS NULL'; + + continue; + } + $join[$i] = "INNER JOIN $meta_table"; $join[$i] .= $i ? " AS $alias" : ''; $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; @@ -732,21 +757,12 @@ class WP_Meta_Query { if ( !empty( $meta_key ) ) $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key ); - if ( !isset( $q['value'] ) ) { + if ( is_null( $meta_value ) ) { if ( empty( $where[$k] ) ) unset( $join[$i] ); continue; } - $meta_value = $q['value']; - - $meta_compare = is_array( $meta_value ) ? 'IN' : '='; - if ( isset( $q['compare'] ) ) - $meta_compare = strtoupper( $q['compare'] ); - - if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) - $meta_compare = '='; - if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { if ( ! is_array( $meta_value ) ) $meta_value = preg_split( '/[,\s]+/', $meta_value );