+ /*
+ * If any JOINs are LEFT JOINs (as in the case of NOT EXISTS), then all JOINs should
+ * be LEFT. Otherwise posts with no metadata will be excluded from results.
+ */
+ if ( false !== strpos( $sql['join'], 'LEFT JOIN' ) ) {
+ $sql['join'] = str_replace( 'INNER JOIN', 'LEFT JOIN', $sql['join'] );
+ }
+
+ /**
+ * Filter 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.
+ * }
+ */
+ 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.
+ *
+ * @since 4.1.0
+ * @access protected
+ *
+ * @return array {
+ * Array containing JOIN and WHERE SQL clauses to append to the main query.
+ *
+ * @type string $join SQL fragment to append to the main JOIN clause.
+ * @type string $where SQL fragment to append to the main WHERE clause.
+ * }
+ */
+ protected function get_sql_clauses() {
+ /*
+ * $queries are passed by reference to get_sql_for_query() for recursion.
+ * To keep $this->queries unaltered, pass a copy.
+ */
+ $queries = $this->queries;
+ $sql = $this->get_sql_for_query( $queries );
+
+ if ( ! empty( $sql['where'] ) ) {
+ $sql['where'] = ' AND ' . $sql['where'];
+ }
+
+ return $sql;
+ }
+
+ /**
+ * Generate SQL clauses for a single query array.
+ *
+ * If nested subqueries are found, this method recurses the tree to
+ * produce the properly nested SQL.
+ *
+ * @since 4.1.0
+ * @access protected
+ *
+ * @param array $query Query to parse, passed by reference.
+ * @param int $depth Optional. Number of tree levels deep we currently are.
+ * Used to calculate indentation. Default 0.
+ * @return array {
+ * Array containing JOIN and WHERE SQL clauses to append to a single query array.
+ *
+ * @type string $join SQL fragment to append to the main JOIN clause.
+ * @type string $where SQL fragment to append to the main WHERE clause.
+ * }
+ */
+ protected function get_sql_for_query( &$query, $depth = 0 ) {
+ $sql_chunks = array(
+ 'join' => array(),
+ 'where' => array(),
+ );
+
+ $sql = array(
+ 'join' => '',
+ 'where' => '',
+ );
+
+ $indent = '';
+ for ( $i = 0; $i < $depth; $i++ ) {
+ $indent .= " ";