Make WordPress Core

Ticket #25284: 25284.diff

File 25284.diff, 4.9 KB (added by helen, 12 years ago)
  • tests/phpunit/tests/term/query.php

     
    6262
    6363                $this->assertEquals( array( $image_id ), $posts );
    6464        }
    65 }
    66  No newline at end of file
     65
     66        /**
     67         * @ticket 25284
     68         */
     69        function test_term_taxonomy_id_field_no_taxonomy() {
     70                $posts = $this->factory->post->create_many( 5 );
     71
     72                $cats = $tags = array();
     73
     74                // need term_taxonomy_ids in addition to term_ids, so no factory
     75                for ( $i = 0; $i < 5; $i++ ) {
     76                        $cats[$i] = wp_insert_term( 'category-' . $i , 'category' );
     77                        $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' );
     78
     79                        // post 0 gets all terms
     80                        wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true );
     81                        wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true );
     82                }
     83
     84                wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     85                wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );
     86
     87                wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );
     88                wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     89
     90                wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
     91                wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
     92
     93                $results = $this->q->query( array(
     94                        'fields' => 'ids',
     95                        'orderby' => 'id',
     96                        'order' => 'ASC',
     97                        'tax_query' => array(
     98                                'relation' => 'OR',
     99                                array(
     100                                        'field' => 'term_taxonomy_id',
     101                                        'terms' => array( $cats[0]['term_taxonomy_id'], $cats[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'], $tags[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'] ),
     102                                        'operator' => 'AND',
     103                                        'include_children' => false,
     104                                ),
     105                                array(
     106                                        'field' => 'term_taxonomy_id',
     107                                        'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     108                                        'operator' => 'AND',
     109                                        'include_children' => false,
     110                                )
     111                        )
     112                ) );
     113
     114                $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results, 'Relation: OR; Operator: AND' );
     115
     116                $results = $this->q->query( array(
     117                        'fields' => 'ids',
     118                        'orderby' => 'id',
     119                        'order' => 'ASC',
     120                        'tax_query' => array(
     121                                'relation' => 'AND',
     122                                array(
     123                                        'field' => 'term_taxonomy_id',
     124                                        'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),
     125                                        'operator' => 'IN',
     126                                        'include_children' => false,
     127                                ),
     128                                array(
     129                                        'field' => 'term_taxonomy_id',
     130                                        'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
     131                                        'operator' => 'IN',
     132                                        'include_children' => false,
     133                                )
     134                        )
     135                ) );
     136
     137                $this->assertEquals( array( $posts[0], $posts[3] ), $results, 'Relation: AND; Operator: IN' );
     138        }
     139}
  • src/wp-includes/taxonomy.php

     
    769769         * @param array &$query The single query
    770770         */
    771771        private function clean_query( &$query ) {
    772                 if ( ! taxonomy_exists( $query['taxonomy'] ) ) {
     772                global $wpdb;
     773
     774                if ( empty( $query['taxonomy'] ) ) {
     775                        if ( 'term_taxonomy_id' !== $query['field'] ) {
     776                                $query = new WP_Error( 'Invalid taxonomy' );
     777                                return;
     778                        }
     779                }
     780                elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) {
    773781                        $query = new WP_Error( 'Invalid taxonomy' );
    774782                        return;
    775783                }
    776784
    777785                $query['terms'] = array_unique( (array) $query['terms'] );
    778786
    779                 if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) {
    780                         $this->transform_query( $query, 'term_id' );
    781 
    782                         if ( is_wp_error( $query ) )
     787                if ( $query['include_children'] ) {
     788                        if ( ! empty( $query['taxonomy'] ) && ! is_taxonomy_hierarchical( $query['taxonomy'] ) )
    783789                                return;
    784790
     791                        if ( ! empty( $query['taxonomy'] ) ) {
     792                                $this->transform_query( $query, 'term_id' );
     793
     794                                if ( is_wp_error( $query ) )
     795                                        return;
     796                        }
     797
    785798                        $children = array();
    786799                        foreach ( $query['terms'] as $term ) {
    787                                 $children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) );
     800                                if ( empty( $query['taxonomy'] ) ) {
     801                                        $taxonomy = $wpdb->get_var( $wpdb->prepare( "SELECT tt.taxonomy FROM $wpdb->term_taxonomy AS tt WHERE tt.term_taxonomy_id = %d", $term ) );
     802
     803                                        if ( $taxonomy )
     804                                                $children = array_merge( $children, get_term_children( $term, $taxonomy ) );
     805                                }
     806                                else {
     807                                        $children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) );
     808                                }
     809
    788810                                $children[] = $term;
    789811                        }
    790812                        $query['terms'] = $children;