From 36658f37cac1fac7e4c5f22f9abaa92f1b3adc82 Mon Sep 17 00:00:00 2001 From: scribu Date: Sat, 20 Nov 2010 21:28:50 +0000 Subject: [PATCH] Fix logic for when excluding a non-existant term. See #12891 git-svn-id: http://svn.automattic.com/wordpress/trunk@16512 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/query.php | 8 ++------ wp-includes/taxonomy.php | 21 +++++++++++++-------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/wp-includes/query.php b/wp-includes/query.php index 09c507b47..756e5e5c5 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -1947,12 +1947,8 @@ class WP_Query { if ( !empty( $this->tax_query ) ) { $clauses = call_user_func_array( 'get_tax_sql', array( $this->tax_query, $wpdb->posts, 'ID', &$this) ); - if ( empty($clauses['join']) && empty($clauses['where']) ) { - $where .= ' AND 0 = 1'; - } else { - $join .= $clauses['join']; - $where .= $clauses['where']; - } + $join .= $clauses['join']; + $where .= $clauses['where']; if ( $this->is_tax ) { if ( empty($post_type) ) { diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 977e9bf16..b2cb9543b 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -552,9 +552,6 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { if ( is_taxonomy_hierarchical( $taxonomy ) && $include_children ) { _transform_terms( $terms, $taxonomies, $field, 'term_id' ); - if ( empty( $terms ) ) - continue; - $children = array(); foreach ( $terms as $term ) { $children = array_merge( $children, get_term_children( $term, $taxonomy ) ); @@ -568,12 +565,12 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { _transform_terms( $terms, $taxonomies, $field, 'term_taxonomy_id' ); } - if ( empty( $terms ) ) - continue; - - $terms = implode( ',', $terms ); - if ( 'IN' == $operator ) { + if ( empty( $terms ) ) + return array( 'join' => '', 'where' => ' AND 0 = 1'); + + $terms = implode( ',', $terms ); + $alias = $i ? 'tt' . $i : $wpdb->term_relationships; $join .= " INNER JOIN $wpdb->term_relationships"; @@ -585,6 +582,11 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { $i++; } elseif ( 'NOT IN' == $operator ) { + if ( empty( $terms ) ) + continue; + + $terms = implode( ',', $terms ); + $where .= " AND $primary_table.$primary_id_column NOT IN ( SELECT object_id FROM $wpdb->term_relationships @@ -599,6 +601,9 @@ function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) { function _transform_terms( &$terms, $taxonomies, $field, $resulting_field ) { global $wpdb; + if ( empty( $terms ) ) + return; + if ( $field == $resulting_field ) return;