<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[46188] trunk: Query: Expand the list of operators available to `compare_key` in `WP_Meta_Query`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/46188">46188</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/46188","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>boonebgorges</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-09-19 15:02:20 +0000 (Thu, 19 Sep 2019)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Query: Expand the list of operators available to `compare_key` in `WP_Meta_Query`.

`compare_key`, introduced in <a href="https://core.trac.wordpress.org/ticket/42409">#42409</a>, previously supported only `=` and `LIKE`
operators. This changeset introduces a number of other operators: `!=`, `IN`,
`NOT IN`, `NOT LIKE`, `RLIKE`, `REGEXP`, `NOT REGEXP`, `EXISTS`, and `NOT EXISTS`
(the latter two aliased to `=` and `!=`, respectively). To support case-sensitive
regular expression key comparisons, the new `type_key` parameter will force
a MySQL `CAST` when 'BINARY' is passed.

Props soulseekah.
Fixes <a href="https://core.trac.wordpress.org/ticket/43346">#43346</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpmetaqueryphp">trunk/src/wp-includes/class-wp-meta-query.php</a></li>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#trunktestsphpunittestsmetaqueryphp">trunk/tests/phpunit/tests/meta/query.php</a></li>
<li><a href="#trunktestsphpunittestsquerymetaQueryphp">trunk/tests/phpunit/tests/query/metaQuery.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpmetaqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-meta-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-meta-query.php     2019-09-19 15:00:14 UTC (rev 46187)
+++ trunk/src/wp-includes/class-wp-meta-query.php       2019-09-19 15:02:20 UTC (rev 46188)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -100,6 +100,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 3.2.0
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.2.0 Introduced support for naming query clauses by associative array keys.
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.1.0 Introduced $compare_key clause parameter, which enables LIKE key matches.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 5.3.0 Increased the number of operators available to $compare_key. Introduced $type_key,
+        *              which enables the $key to be cast to a new data type for comparisons.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param array $meta_query {
</span><span class="cx" style="display: block; padding: 0 10px">         *     Array of meta query clauses. When first-order clauses or sub-clauses use strings as
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -111,8 +113,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *         Optional. An array of first-order clause parameters, or another fully-formed meta query.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         *         @type string $key         Meta key to filter by.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         *         @type string $compare_key MySQL operator used for comparing the $key. Accepts '=' and 'LIKE'.
-        *                                   Default '='.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  *         @type string $compare_key MySQL operator used for comparing the $key. Accepts '=', '!='
+        *                                   'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'REGEXP', 'NOT REGEXP', 'RLIKE',
+        *                                   'EXISTS' (alias of '=') or 'NOT EXISTS' (alias of '!=').
+        *                                   Default is 'IN' when `$key` is an array, '=' otherwise.
+        *         @type string $type_key    MySQL data type that the meta_key column will be CAST to for
+        *                                   comparisons. Accepts 'BINARY' for case-sensitive regular expression
+        *                                   comparisons. Default is ''.
</ins><span class="cx" style="display: block; padding: 0 10px">          *         @type string $value       Meta value to filter by.
</span><span class="cx" style="display: block; padding: 0 10px">         *         @type string $compare     MySQL operator used for comparing the $value. Accepts '=',
</span><span class="cx" style="display: block; padding: 0 10px">         *                                   '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -239,7 +246,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * the rest of the meta_query).
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                $primary_meta_query = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( array( 'key', 'compare', 'type', 'compare_key' ) as $key ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         foreach ( array( 'key', 'compare', 'type', 'compare_key', 'type_key' ) as $key ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! empty( $qv[ "meta_$key" ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $primary_meta_query[ $key ] = $qv[ "meta_$key" ];
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -498,34 +505,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! in_array(
-                       $clause['compare'],
-                       array(
-                               '=',
-                               '!=',
-                               '>',
-                               '>=',
-                               '<',
-                               '<=',
-                               'LIKE',
-                               'NOT LIKE',
-                               'IN',
-                               'NOT IN',
-                               'BETWEEN',
-                               'NOT BETWEEN',
-                               'EXISTS',
-                               'NOT EXISTS',
-                               'REGEXP',
-                               'NOT REGEXP',
-                               'RLIKE',
-                       )
-               ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $non_numeric_operators = array(
+                       '=',
+                       '!=',
+                       'LIKE',
+                       'NOT LIKE',
+                       'IN',
+                       'NOT IN',
+                       'EXISTS',
+                       'NOT EXISTS',
+                       'RLIKE',
+                       'REGEXP',
+                       'NOT REGEXP',
+               );
+
+               $numeric_operators = array(
+                       '>',
+                       '>=',
+                       '<',
+                       '<=',
+                       'BETWEEN',
+                       'NOT BETWEEN',
+               );
+
+               if ( ! in_array( $clause['compare'], $non_numeric_operators, true ) && ! in_array( $clause['compare'], $numeric_operators, true ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $clause['compare'] = '=';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( isset( $clause['compare_key'] ) && 'LIKE' === strtoupper( $clause['compare_key'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( isset( $clause['compare_key'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $clause['compare_key'] = strtoupper( $clause['compare_key'] );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        $clause['compare_key'] = isset( $clause['key'] ) && is_array( $clause['key'] ) ? 'IN' : '=';
+               }
+
+               if ( ! in_array( $clause['compare_key'], $non_numeric_operators, true ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $clause['compare_key'] = '=';
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -594,11 +607,79 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( 'NOT EXISTS' === $meta_compare ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
</span><span class="cx" style="display: block; padding: 0 10px">                        } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                if ( 'LIKE' === $meta_compare_key ) {
-                                       $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key LIKE %s", '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%' );
-                               } else {
-                                       $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         /**
+                                * In joined clauses negative operators have to be nested into a
+                                * NOT EXISTS clause and flipped, to avoid returning records with
+                                * matching post IDs but different meta keys. Here we prepare the
+                                * nested clause.
+                                */
+                               if ( in_array( $meta_compare_key, array( '!=', 'NOT IN', 'NOT LIKE', 'NOT EXISTS', 'NOT REGEXP' ), true ) ) {
+                                       // Negative clauses may be reused.
+                                       $i                     = count( $this->table_aliases );
+                                       $subquery_alias        = $i ? 'mt' . $i : $this->meta_table;
+                                       $this->table_aliases[] = $subquery_alias;
+
+                                       $meta_compare_string_start  = 'NOT EXISTS (';
+                                       $meta_compare_string_start .= "SELECT 1 FROM $wpdb->postmeta $subquery_alias ";
+                                       $meta_compare_string_start .= "WHERE $subquery_alias.post_ID = $alias.post_ID ";
+                                       $meta_compare_string_end    = 'LIMIT 1';
+                                       $meta_compare_string_end   .= ')';
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               switch ( $meta_compare_key ) {
+                                       case '=':
+                                       case 'EXISTS':
+                                               $where = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+                                               break;
+                                       case 'LIKE':
+                                               $meta_compare_value = '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%';
+                                               $where              = $wpdb->prepare( "$alias.meta_key LIKE %s", $meta_compare_value ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+                                               break;
+                                       case 'IN':
+                                               $meta_compare_string = "$alias.meta_key IN (" . substr( str_repeat( ',%s', count( $clause['key'] ) ), 1 ) . ')';
+                                               $where               = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+                                               break;
+                                       case 'RLIKE':
+                                       case 'REGEXP':
+                                               $operator = $meta_compare_key;
+                                               if ( isset( $clause['type_key'] ) && 'BINARY' === strtoupper( $clause['type_key'] ) ) {
+                                                       $cast = 'BINARY';
+                                               } else {
+                                                       $cast = '';
+                                               }
+                                               $where = $wpdb->prepare( "$alias.meta_key $operator $cast %s", trim( $clause['key'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+                                               break;
+
+                                       case '!=':
+                                       case 'NOT EXISTS':
+                                               $meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key = %s " . $meta_compare_string_end;
+                                               $where               = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+                                               break;
+                                       case 'NOT LIKE':
+                                               $meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key LIKE %s " . $meta_compare_string_end;
+
+                                               $meta_compare_value = '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%';
+                                               $where              = $wpdb->prepare( $meta_compare_string, $meta_compare_value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+                                               break;
+                                       case 'NOT IN':
+                                               $array_subclause     = '(' . substr( str_repeat( ',%s', count( $clause['key'] ) ), 1 ) . ') ';
+                                               $meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key IN " . $array_subclause . $meta_compare_string_end;
+                                               $where               = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+                                               break;
+                                       case 'NOT REGEXP':
+                                               $operator = $meta_compare_key;
+                                               if ( isset( $clause['type_key'] ) && 'BINARY' === strtoupper( $clause['type_key'] ) ) {
+                                                       $cast = 'BINARY';
+                                               } else {
+                                                       $cast = '';
+                                               }
+
+                                               $meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key REGEXP $cast %s " . $meta_compare_string_end;
+                                               $where               = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
+                                               break;
+                               }
+
+                               $sql_chunks['where'][] = $where;
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpincludesclasswpqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php  2019-09-19 15:00:14 UTC (rev 46187)
+++ trunk/src/wp-includes/class-wp-query.php    2019-09-19 15:02:20 UTC (rev 46188)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -614,6 +614,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument.
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.9.0 Introduced the `$comment_count` parameter.
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.1.0 Introduced the `$meta_compare_key` parameter.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 5.3.0 Introduced the `$meta_type_key` parameter.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @param string|array $query {
</span><span class="cx" style="display: block; padding: 0 10px">         *     Optional. Array or string of Query parameters.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -655,6 +656,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type array        $meta_query              An associative array of WP_Meta_Query arguments. See WP_Meta_Query.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string       $meta_value              Custom field value.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int          $meta_value_num          Custom field value number.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         *     @type string       $meta_type_key           Cast for 'meta_key'. See WP_Meta_Query::construct().
</ins><span class="cx" style="display: block; padding: 0 10px">          *     @type int          $menu_order              The menu order of the posts.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int          $monthnum                The two-digit month. Default empty. Accepts numbers 1-12.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string       $name                    Post slug.
</span></span></pre></div>
<a id="trunktestsphpunittestsmetaqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/meta/query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/meta/query.php  2019-09-19 15:00:14 UTC (rev 46187)
+++ trunk/tests/phpunit/tests/meta/query.php    2019-09-19 15:02:20 UTC (rev 46188)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -738,6 +738,42 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Verifies only that meta_type_key is passed. See query/metaQuery.php for more complete tests.
+        *
+        * @ticket 43446
+        */
+       public function test_meta_type_key_should_be_passed_to_meta_query() {
+               $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' );
+               add_post_meta( $posts[2], 'aaa_foo_aaa', 'abc' );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_key'         => 'AAA_foo_.*',
+                               'meta_compare_key' => 'REGEXP',
+                               'fields'           => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[0], $posts[2] ), $q->posts );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_key'         => 'AAA_FOO_.*',
+                               'meta_compare_key' => 'REGEXP',
+                               'meta_type_key'    => 'BINARY',
+                               'fields'           => 'ids',
+                               'fields'           => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[0] ), $q->posts );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * This is the clause that ensures that empty arrays are not valid queries.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_get_sql_null_value_and_empty_key_should_not_have_table_join() {
</span></span></pre></div>
<a id="trunktestsphpunittestsquerymetaQueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/query/metaQuery.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query/metaQuery.php     2019-09-19 15:00:14 UTC (rev 46187)
+++ trunk/tests/phpunit/tests/query/metaQuery.php       2019-09-19 15:02:20 UTC (rev 46188)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1922,4 +1922,247 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertEqualSets( array( $posts[0] ), $q->posts );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 43446
+        */
+       public function test_compare_key_not_equals() {
+               $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' );
+               add_post_meta( $posts[2], 'aaa_foo_ccc', 'abc' );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => '!=',
+                                               'key'         => 'aaa_foo_bbb',
+                                               'value'       => 'abc',
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[0], $posts[1] ), $q->posts );
+       }
+
+       /**
+        * @ticket 43446
+        */
+       public function test_compare_key_not_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[1], 'aaa_bar_ccc', 'abc' );
+               add_post_meta( $posts[2], 'aaa_foo_bbb', 'abc' );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => 'NOT LIKE',
+                                               'key'         => 'aaa_bar',
+                                               'value'       => 'abc',
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[0], $posts[2] ), $q->posts );
+       }
+
+       /**
+        * @ticket 43446
+        */
+       public function test_compare_key_in() {
+               $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' => 'IN',
+                                               'key'         => array( 'aaa_foo_bbb', 'aaa_bar_aaa' ),
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[1], $posts[2] ), $q->posts );
+       }
+
+       /**
+        * @ticket 43446
+        */
+       public function test_compare_key_not_in() {
+               $posts = self::factory()->post->create_many( 3 );
+
+               add_post_meta( $posts[0], 'aaa_foo_aaa', 'abc' );
+               add_post_meta( $posts[0], 'aaa_foo_ddd', 'abc' );
+               add_post_meta( $posts[1], 'aaa_bar_aaa', 'abc' );
+               add_post_meta( $posts[2], 'aaa_foo_bbb', 'abc' );
+               add_post_meta( $posts[2], 'aaa_foo_ccc', 'abc' );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => 'NOT IN',
+                                               'key'         => array( 'aaa_foo_bbb', 'aaa_foo_ddd' ),
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[1] ), $q->posts );
+       }
+
+       /**
+        * @ticket 43446
+        */
+       public function test_compare_key_not_exists() {
+               $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' );
+               add_post_meta( $posts[2], 'aaa_foo_ccc', 'abc' );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => 'NOT EXISTS',
+                                               'key'         => 'aaa_foo_bbb',
+                                               'value'       => 'abc',
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[0], $posts[1] ), $q->posts );
+       }
+
+       /**
+        * @ticket 43446
+        */
+       public function test_compare_key_exists() {
+               $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' );
+               add_post_meta( $posts[2], 'aaa_foo_ccc', 'abc' );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => 'EXISTS',
+                                               'key'         => 'aaa_foo_bbb',
+                                               'value'       => 'abc',
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[2] ), $q->posts );
+       }
+
+       /**
+        * @ticket 43446
+        */
+       public function test_compare_key_regexp_rlike() {
+               $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' );
+               add_post_meta( $posts[2], 'aaa_foo_aaa', 'abc' );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => 'REGEXP',
+                                               'key'         => 'AAA_foo_.*',
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[0], $posts[2] ), $q->posts );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => 'RLIKE',
+                                               'key'         => 'AAA_FOO_.*',
+                                               'type_key'    => 'BINARY',
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[0] ), $q->posts );
+       }
+
+       /**
+        * @ticket 43446
+        */
+       public function test_compare_key_not_regexp() {
+               $posts = self::factory()->post->create_many( 3 );
+
+               add_post_meta( $posts[0], 'AAA_FOO_AAA', 'abc' );
+               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' );
+               add_post_meta( $posts[2], 'aaa_foo_aaa', 'abc' );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => 'NOT REGEXP',
+                                               'key'         => 'AAA_foo_.*',
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[1] ), $q->posts );
+
+               $q = new WP_Query(
+                       array(
+                               'meta_query' => array(
+                                       array(
+                                               'compare_key' => 'NOT REGEXP',
+                                               'key'         => 'AAA_FOO_.*',
+                                               'type_key'    => 'BINARY',
+                                       ),
+                               ),
+                               'fields'     => 'ids',
+                       )
+               );
+
+               $this->assertEqualSets( array( $posts[1], $posts[2] ), $q->posts );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>