Changeset 28562
- Timestamp:
- 05/23/2014 07:58:52 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/taxonomy.php
r28539 r28562 633 633 /** 634 634 * List of taxonomy queries. A single taxonomy query is an associative array: 635 * - 'taxonomy' string The taxonomy being queried 635 * - 'taxonomy' string The taxonomy being queried. Optional when using the term_taxonomy_id field. 636 636 * - 'terms' string|array The list of terms 637 637 * - 'field' string (optional) Which term field is being used. 638 * Possible values: 'term_id', 'slug' or 'name'638 * Possible values: 'term_id', 'slug', 'name', or 'term_taxonomy_id' 639 639 * Default: 'term_id' 640 640 * - 'operator' string (optional) 641 641 * Possible values: 'AND', 'IN' or 'NOT IN'. 642 642 * Default: 'IN' 643 * - 'include_children' bool (optional) Whether to include child terms. 643 * - 'include_children' bool (optional) Whether to include child terms. Requires that a taxonomy be specified. 644 644 * Default: true 645 645 * … … 819 819 */ 820 820 private function clean_query( &$query ) { 821 if ( ! taxonomy_exists( $query['taxonomy'] ) ) { 821 if ( empty( $query['taxonomy'] ) ) { 822 if ( 'term_taxonomy_id' !== $query['field'] ) { 823 $query = new WP_Error( 'Invalid taxonomy' ); 824 return; 825 } 826 827 // so long as there are shared terms, include_children requires that a taxonomy is set 828 $query['include_children'] = false; 829 } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) { 822 830 $query = new WP_Error( 'Invalid taxonomy' ); 823 831 return; -
trunk/tests/phpunit/tests/term/query.php
r28188 r28562 99 99 $this->assertEquals( array( $post_id1, $post_id2 ), $ids ); 100 100 } 101 102 function test_tax_query_no_taxonomy() { 103 $cat_id = $this->factory->category->create( array( 'name' => 'alpha' ) ); 104 $this->factory->post->create( array( 'post_title' => 'alpha', 'post_category' => array( $cat_id ) ) ); 105 106 $response1 = new WP_Query( array( 107 'tax_query' => array( 108 array( 'terms' => array( $cat_id ) ) 109 ) 110 ) ); 111 $this->assertEmpty( $response1->posts ); 112 113 $response2 = new WP_Query( array( 114 'tax_query' => array( 115 array( 116 'taxonomy' => 'category', 117 'terms' => array( $cat_id ) 118 ) 119 ) 120 ) ); 121 $this->assertNotEmpty( $response2->posts ); 122 123 $term = get_category( $cat_id ); 124 $response3 = new WP_Query( array( 125 'tax_query' => array( 126 array( 127 'field' => 'term_taxonomy_id', 128 'terms' => array( $term->term_taxonomy_id ) 129 ) 130 ) 131 ) ); 132 $this->assertNotEmpty( $response3->posts ); 133 } 134 135 function test_term_taxonomy_id_field_no_taxonomy() { 136 $posts = $this->factory->post->create_many( 5 ); 137 138 $cats = $tags = array(); 139 140 // need term_taxonomy_ids in addition to term_ids, so no factory 141 for ( $i = 0; $i < 5; $i++ ) { 142 $cats[$i] = wp_insert_term( 'category-' . $i , 'category' ); 143 $tags[$i] = wp_insert_term( 'tag-' . $i, 'post_tag' ); 144 145 // post 0 gets all terms 146 wp_set_object_terms( $posts[0], array( $cats[$i]['term_id'] ), 'category', true ); 147 wp_set_object_terms( $posts[0], array( $tags[$i]['term_id'] ), 'post_tag', true ); 148 } 149 150 wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); 151 wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' ); 152 153 wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' ); 154 wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); 155 156 wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' ); 157 wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' ); 158 159 $results1 = $this->q->query( array( 160 'fields' => 'ids', 161 'orderby' => 'id', 162 'order' => 'ASC', 163 'tax_query' => array( 164 'relation' => 'OR', 165 array( 166 'field' => 'term_taxonomy_id', 167 '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'] ), 168 'operator' => 'AND', 169 'include_children' => false, 170 ), 171 array( 172 'field' => 'term_taxonomy_id', 173 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), 174 'operator' => 'AND', 175 'include_children' => false, 176 ) 177 ) 178 ) ); 179 180 $this->assertEquals( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' ); 181 182 $results2 = $this->q->query( array( 183 'fields' => 'ids', 184 'orderby' => 'id', 185 'order' => 'ASC', 186 'tax_query' => array( 187 'relation' => 'AND', 188 array( 189 'field' => 'term_taxonomy_id', 190 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ), 191 'operator' => 'IN', 192 'include_children' => false, 193 ), 194 array( 195 'field' => 'term_taxonomy_id', 196 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ), 197 'operator' => 'IN', 198 'include_children' => false, 199 ) 200 ) 201 ) ); 202 203 $this->assertEquals( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' ); 204 } 101 205 }
Note: See TracChangeset
for help on using the changeset viewer.