Make WordPress Core

Changeset 37184


Ignore:
Timestamp:
04/12/2016 08:36:31 PM (10 years ago)
Author:
boonebgorges
Message:

Use LEFT JOIN when building WP_Tax_Query SQL.

LEFT JOIN ensures that NOT EXISTS queries will not miss posts that have
no taxonomy data whatsoever.

Props swissspidy, crstauf.
Fixes #36343.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-tax-query.php

    r35916 r37184  
    441441                $clause['alias'] = $alias;
    442442
    443                 $join .= " INNER JOIN $wpdb->term_relationships";
     443                $join .= " LEFT JOIN $wpdb->term_relationships";
    444444                $join .= $i ? " AS $alias" : '';
    445445                $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
  • trunk/tests/phpunit/tests/query/taxQuery.php

    r35242 r37184  
    408408
    409409        $this->assertEqualSets( array( $p1, $p3 ), $q->posts );
     410    }
     411
     412    /**
     413     * @ticket 36343
     414     */
     415    public function test_tax_query_operator_not_exists_combined() {
     416        register_post_type( 'wptests_cpt1' );
     417        register_taxonomy( 'wptests_tax1', 'wptests_cpt1' );
     418        register_taxonomy( 'wptests_tax2', 'wptests_cpt1' );
     419
     420        $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     421        $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
     422        $t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
     423
     424        $p1 = self::factory()->post->create( array( 'post_type' => 'wptests_cpt1' ) );
     425        $p2 = self::factory()->post->create( array( 'post_type' => 'wptests_cpt1' ) );
     426        $p3 = self::factory()->post->create( array( 'post_type' => 'wptests_cpt1' ) );
     427        $p4 = self::factory()->post->create( array( 'post_type' => 'wptests_cpt1' ) );
     428
     429        wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
     430        wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax1' );
     431        wp_set_object_terms( $p3, array( $t3 ), 'wptests_tax2' );
     432
     433        $q = new WP_Query( array(
     434            'post_type' => 'wptests_cpt1',
     435            'fields'    => 'ids',
     436            'tax_query' => array(
     437                'relation' => 'OR',
     438                array(
     439                    'taxonomy' => 'wptests_tax1',
     440                    'operator' => 'NOT EXISTS',
     441                ),
     442                array(
     443                    'taxonomy' => 'wptests_tax1',
     444                    'field'    => 'slug',
     445                    'terms'    => get_term_field( 'slug', $t1 ),
     446                ),
     447            ),
     448        ) );
     449
     450        unregister_post_type( 'wptests_cpt1' );
     451        unregister_taxonomy( 'wptests_tax1' );
     452        unregister_taxonomy( 'wptests_tax2' );
     453
     454        $this->assertEqualSets( array( $p1, $p3, $p4 ), $q->posts );
    410455    }
    411456
Note: See TracChangeset for help on using the changeset viewer.