Changeset 44452
- Timestamp:
- 01/08/2019 03:32:04 AM (6 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r44276 r44452 1547 1547 'comment_count', 1548 1548 'rand', 1549 'post__in', 1550 'post_parent__in', 1551 'post_name__in', 1549 1552 ); 1550 1553 … … 1576 1579 return false; 1577 1580 } 1581 1582 $orderby_clause = ''; 1578 1583 1579 1584 switch ( $orderby ) { … … 1604 1609 $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0"; 1605 1610 break; 1611 case 'post__in': 1612 if ( ! empty( $this->query_vars['post__in'] ) ) { 1613 $orderby_clause = "FIELD({$wpdb->posts}.ID," . implode( ',', array_map( 'absint', $this->query_vars['post__in'] ) ) . ')'; 1614 } 1615 break; 1616 case 'post_parent__in': 1617 if ( ! empty( $this->query_vars['post_parent__in'] ) ) { 1618 $orderby_clause = "FIELD( {$wpdb->posts}.post_parent," . implode( ', ', array_map( 'absint', $this->query_vars['post_parent__in'] ) ) . ' )'; 1619 } 1620 break; 1621 case 'post_name__in': 1622 if ( ! empty( $this->query_vars['post_name__in'] ) ) { 1623 $post_name__in = array_map( 'sanitize_title_for_query', $this->query_vars['post_name__in'] ); 1624 $post_name__in_string = "'" . implode( "','", $post_name__in ) . "'"; 1625 $orderby_clause = "FIELD( {$wpdb->posts}.post_name," . $post_name__in_string . ' )'; 1626 } 1627 break; 1606 1628 default: 1607 1629 if ( array_key_exists( $orderby, $meta_clauses ) ) { … … 2240 2262 } 2241 2263 2264 // These values of orderby should ignore the 'order' parameter. 2265 $force_asc = array( 'post__in', 'post_name__in', 'post_parent__in' ); 2266 if ( isset( $q['orderby'] ) && in_array( $q['orderby'], $force_asc, true ) ) { 2267 $q['order'] = ''; 2268 } 2269 2242 2270 // Order by. 2243 2271 if ( empty( $q['orderby'] ) ) { … … 2253 2281 } elseif ( 'none' == $q['orderby'] ) { 2254 2282 $orderby = ''; 2255 } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {2256 $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";2257 } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {2258 $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";2259 } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) {2260 $orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )";2261 2283 } else { 2262 2284 $orderby_array = array(); -
trunk/tests/phpunit/tests/post/query.php
r43571 r44452 172 172 ) 173 173 ); 174 $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) ); 175 } 176 177 /** 178 * @ticket 38034 179 */ 180 public function test_orderby_post__in_array() { 181 $posts = self::factory()->post->create_many( 4 ); 182 183 $ordered = array( $posts[2], $posts[0], $posts[3] ); 184 185 $q = new WP_Query( array( 186 'post_type' => 'any', 187 'post__in' => $ordered, 188 'orderby' => array( 'post__in' => 'ASC' ), 189 ) ); 190 $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) ); 191 } 192 193 /** 194 * @ticket 38034 195 */ 196 public function test_orderby_post__in_array_with_implied_order() { 197 $posts = self::factory()->post->create_many( 4 ); 198 199 $ordered = array( $posts[2], $posts[0], $posts[3] ); 200 201 $q = new WP_Query( array( 202 'post_type' => 'any', 203 'post__in' => $ordered, 204 'orderby' => 'post__in', 205 ) ); 174 206 $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) ); 175 207 } -
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r44394 r44452 292 292 $this->assertEquals( 2, count( $data ) ); 293 293 $this->assertEquals( $id1, $data[0]['id'] ); 294 $this->assertPostsOrderedBy( "FIELD( {posts}.ID, $id1,$id3)" );294 $this->assertPostsOrderedBy( "FIELD({posts}.ID,$id1,$id3)" ); 295 295 // Invalid include should error 296 296 $request = new WP_REST_Request( 'GET', '/wp/v2/posts' );
Note: See TracChangeset
for help on using the changeset viewer.