Make WordPress Core

Ticket #25284: 25284.2.diff

File 25284.2.diff, 4.7 KB (added by helen, 11 years ago)
  • src/wp-includes/taxonomy.php

     
    626626
    627627        /**
    628628         * List of taxonomy queries. A single taxonomy query is an associative array:
    629          * - 'taxonomy' string The taxonomy being queried
     629         * - 'taxonomy' string The taxonomy being queried. Optional when using the term_taxonomy_id field.
    630630         * - 'terms' string|array The list of terms
    631631         * - 'field' string (optional) Which term field is being used.
    632          *              Possible values: 'term_id', 'slug' or 'name'
     632         *              Possible values: 'term_id', 'slug', 'name', or 'term_taxonomy_id'
    633633         *              Default: 'term_id'
    634634         * - 'operator' string (optional)
    635635         *              Possible values: 'AND', 'IN' or 'NOT IN'.
    636636         *              Default: 'IN'
    637          * - 'include_children' bool (optional) Whether to include child terms.
     637         * - 'include_children' bool (optional) Whether to include child terms. Requires that a taxonomy be specified.
    638638         *              Default: true
    639639         *
    640640         * @since 3.1.0
     
    807807         * @param array &$query The single query
    808808         */
    809809        private function clean_query( &$query ) {
    810                 if ( ! taxonomy_exists( $query['taxonomy'] ) ) {
     810                if ( empty( $query['taxonomy'] ) ) {
     811                        if ( 'term_taxonomy_id' !== $query['field'] ) {
     812                                $query = new WP_Error( 'Invalid taxonomy' );
     813                                return;
     814                        }
     815
     816                        // so long as there are shared terms, include_children requires that a taxonomy is set
     817                        $query['include_children'] = false;
     818                } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) {
    811819                        $query = new WP_Error( 'Invalid taxonomy' );
    812820                        return;
    813821                }
  • 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}