*
* Used for generating SQL clauses that filter a primary query according to metadata keys and values.
*
- * `WP_Meta_Query` is a helper that allows primary query classes, such as {@see WP_Query} and {@see WP_User_Query},
+ * WP_Meta_Query is a helper that allows primary query classes, such as WP_Query and WP_User_Query,
+ *
* to filter their results by object metadata, by generating `JOIN` and `WHERE` subclauses to be attached
* to the primary SQL query string.
*
/**
* Array of metadata queries.
*
- * See {@see WP_Meta_Query::__construct()} for information on meta query arguments.
+ * See WP_Meta_Query::__construct() for information on meta query arguments.
*
* @since 3.2.0
* @access public
* @access public
*
* @param array $meta_query {
- * Array of meta query clauses. When first-order clauses use strings as their array keys, they may be
- * referenced in the 'orderby' parameter of the parent query.
+ * Array of meta query clauses. When first-order clauses or sub-clauses use strings as
+ * their array keys, they may be referenced in the 'orderby' parameter of the parent query.
*
* @type string $relation Optional. The MySQL keyword used to join
* the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
* @type string $key Meta key to filter by.
* @type string $value Meta value to filter by.
* @type string $compare MySQL operator used for comparing the $value. Accepts '=',
- * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN',
- * 'BETWEEN', 'NOT BETWEEN', 'REGEXP', 'NOT REGEXP', or 'RLIKE'.
+ * '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE',
+ * 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'REGEXP',
+ * 'NOT REGEXP', 'RLIKE', 'EXISTS' or 'NOT EXISTS'.
* Default is 'IN' when `$value` is an array, '=' otherwise.
* @type string $type MySQL data type that the meta_value column will be CAST to for
* comparisons. Accepts 'NUMERIC', 'BINARY', 'CHAR', 'DATE',
return false;
}
+ $this->table_aliases = array();
+
$this->meta_table = $meta_table;
$this->meta_id_column = sanitize_key( $type . '_id' );
}
/**
- * Filter the meta query's generated SQL.
+ * Filters the meta query's generated SQL.
*
* @since 3.1.0
*
- * @param array $args {
- * An array of meta query SQL arguments.
- *
- * @type array $clauses Array containing the query's JOIN and WHERE clauses.
- * @type array $queries Array of meta queries.
- * @type string $type Type of meta.
- * @type string $primary_table Primary table.
- * @type string $primary_id_column Primary column ID.
- * @type object $context The main query object.
- * }
+ * @param array $clauses Array containing the query's JOIN and WHERE clauses.
+ * @param array $queries Array of meta queries.
+ * @param string $type Type of meta.
+ * @param string $primary_table Primary table.
+ * @param string $primary_id_column Primary column ID.
+ * @param object $context The main query object.
*/
return apply_filters_ref_array( 'get_meta_sql', array( $sql, $this->queries, $type, $primary_table, $primary_id_column, $context ) );
}
/**
* Generate SQL clauses to be appended to a main query.
*
- * Called by the public {@see WP_Meta_Query::get_sql()}, this method
- * is abstracted out to maintain parity with the other Query classes.
+ * Called by the public WP_Meta_Query::get_sql(), this method is abstracted
+ * out to maintain parity with the other Query classes.
*
* @since 4.1.0
* @access protected
}
if ( $where ) {
- $sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}";
+ if ( 'CHAR' === $meta_type ) {
+ $sql_chunks['where'][] = "$alias.meta_value {$meta_compare} {$where}";
+ } else {
+ $sql_chunks['where'][] = "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$where}";
+ }
}
}
* An existing alias is compatible if (a) it is a sibling of `$clause`
* (ie, it's under the scope of the same relation), and (b) the combination
* of operator and relation between the clauses allows for a shared table join.
- * In the case of {@see WP_Meta_Query}, this only applies to 'IN' clauses that
- * are connected by the relation 'OR'.
+ * In the case of WP_Meta_Query, this only applies to 'IN' clauses that are
+ * connected by the relation 'OR'.
*
* @since 4.1.0
* @access protected
}
/**
- * Filter the table alias identified as compatible with the current clause.
+ * Filters the table alias identified as compatible with the current clause.
*
* @since 4.1.0
*