From 09fbcfc1ad1de86d9eccabe7533ab976802fb4fb Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 15 Oct 2014 00:54:21 +0000 Subject: [PATCH] Support 'EXISTS' and 'NOT EXISTS' in `WP_Tax_Query`. These new values for the 'operator' parameter make it possible to filter items that have no term from a given taxonomy, or any term from a given taxonomy. Includes unit tests. Fixes #29181. Built from https://develop.svn.wordpress.org/trunk@29896 git-svn-id: http://core.svn.wordpress.org/trunk@29651 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy.php | 18 ++++++++++++++++-- wp-includes/version.php | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 713898b307..91f47f17c7 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -691,10 +691,11 @@ class WP_Tax_Query { * Constructor. * * @since 3.1.0 + * @since 4.1.0 Added support for $operator 'NOT EXISTS' and 'EXISTS'. * @access public * * @param array $tax_query { - * Array of taxonoy query clauses. + * Array of taxonomy query clauses. * * @type string $relation Optional. The MySQL keyword used to join * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'. @@ -706,7 +707,8 @@ class WP_Tax_Query { * @type string $field Field to match $terms against. Accepts 'term_id', 'slug', * 'name', or 'term_taxonomy_id'. Default: 'term_id'. * @type string $operator MySQL operator to be used with $terms in the WHERE clause. - * Accepts 'AND', 'IN', or 'OR. Default: 'IN'. + * Accepts 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS'. + * Default: 'IN'. * @type bool $include_children Optional. Whether to include child terms. * Requires a $taxonomy. Default: true. * } @@ -1026,6 +1028,18 @@ class WP_Tax_Query { WHERE term_taxonomy_id IN ($terms) AND object_id = $this->primary_table.$this->primary_id_column ) = $num_terms"; + + } elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) { + + $where = $wpdb->prepare( "$operator ( + SELECT 1 + FROM $wpdb->term_relationships + INNER JOIN $wpdb->term_taxonomy + ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id + WHERE $wpdb->term_taxonomy.taxonomy = %s + AND $wpdb->term_relationships.object_id = $this->primary_table.$this->primary_id_column + )", $clause['taxonomy'] ); + } $sql['join'][] = $join; diff --git a/wp-includes/version.php b/wp-includes/version.php index 3da815700c..7067d2492e 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.1-alpha-20141014'; +$wp_version = '4.1-alpha-20141015'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.