Make WordPress Core

Ticket #29181: 29181.patch

File 29181.patch, 3.4 KB (added by boonebgorges, 11 years ago)
  • src/wp-includes/taxonomy.php

    diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
    index d942734..b65c624 100644
    class WP_Tax_Query { 
    640640         *              Possible values: 'term_id', 'slug', 'name', or 'term_taxonomy_id'
    641641         *              Default: 'term_id'
    642642         * - 'operator' string (optional)
    643          *              Possible values: 'AND', 'IN' or 'NOT IN'.
     643         *              Possible values: 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS'
    644644         *              Default: 'IN'
    645645         * - 'include_children' bool (optional) Whether to include child terms. Requires that a taxonomy be specified.
    646646         *              Default: true
    class WP_Tax_Query { 
    798798                                        WHERE term_taxonomy_id IN ($terms)
    799799                                        AND object_id = $primary_table.$primary_id_column
    800800                                ) = $num_terms";
     801                        } elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) {
     802
     803                                $taxonomy = esc_sql( $query['taxonomy'] );
     804
     805                                $where[] = "$operator (
     806                                        SELECT 1
     807                                        FROM $wpdb->term_relationships
     808                                        INNER JOIN $wpdb->term_taxonomy
     809                                        ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
     810                                        WHERE $wpdb->term_taxonomy.taxonomy = '$taxonomy'
     811                                        AND $wpdb->term_relationships.object_id = $primary_table.$primary_id_column
     812                                )";
    801813                        }
    802814
    803815                        $i++;
  • tests/phpunit/tests/term/query.php

    diff --git tests/phpunit/tests/term/query.php tests/phpunit/tests/term/query.php
    index 0a3c724..0e22b1f 100644
    class Tests_Tax_Query extends WP_UnitTestCase { 
    231231                $q6 = get_posts( array( 'tag_slug__in' => array() ) );
    232232                $this->assertNotEmpty( $q6 );
    233233        }
     234
     235        public function test_operator_not_exists() {
     236                $c1 = $this->factory->category->create();
     237                $t1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'foo' ) );
     238                $t2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'bar' ) );
     239
     240                $p1 = $this->factory->post->create();
     241                $p2 = $this->factory->post->create();
     242                $p3 = $this->factory->post->create();
     243
     244                wp_set_object_terms( $p1, array( $c1 ), 'category' );
     245                wp_set_object_terms( $p2, array( $t1 ), 'post_tag' );
     246                wp_set_object_terms( $p3, array( $t1 ), 'post_tag' );
     247                wp_set_object_terms( $p3, array( $t2 ), 'post_tag' );
     248
     249                $found1 = $this->q->query( array(
     250                        'fields' => 'ids',
     251                        'orderby' => 'ID',
     252                        'order' => 'ASC',
     253                        'tax_query' => array(
     254                                array(
     255                                        'taxonomy' => 'post_tag',
     256                                        'operator' => 'NOT EXISTS',
     257                                ),
     258                        ),
     259                ) );
     260
     261                $this->assertSame( array( $p1 ), $found1 );
     262        }
     263
     264        public function test_operator_exists() {
     265                $c1 = $this->factory->category->create();
     266                $t1 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'foo' ) );
     267                $t2 = $this->factory->term->create( array( 'taxonomy' => 'post_tag', 'name' => 'bar' ) );
     268                $p1 = $this->factory->post->create();
     269                $p2 = $this->factory->post->create();
     270                $p3 = $this->factory->post->create();
     271
     272                wp_set_object_terms( $p1, array( $c1 ), 'category' );
     273                wp_set_object_terms( $p2, array( $t1 ), 'post_tag' );
     274                wp_set_object_terms( $p3, array( $t1 ), 'post_tag' );
     275                wp_set_object_terms( $p3, array( $t2 ), 'post_tag' );
     276
     277                $found1 = $this->q->query( array(
     278                        'fields' => 'ids',
     279                        'orderby' => 'ID',
     280                        'order' => 'ASC',
     281                        'tax_query' => array(
     282                                array(
     283                                        'taxonomy' => 'post_tag',
     284                                        'operator' => 'EXISTS',
     285                                ),
     286                        ),
     287                ) );
     288
     289                $this->assertSame( array( $p2, $p3 ), $found1 );
     290        }
    234291}