diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php
index e569e02ef1..2c044f3649 100644
|
|
|
class WP_Query {
|
| 1546 | 1546 | 'menu_order', |
| 1547 | 1547 | 'comment_count', |
| 1548 | 1548 | 'rand', |
| | 1549 | 'post__in', |
| 1549 | 1550 | ); |
| 1550 | 1551 | |
| 1551 | 1552 | $primary_meta_key = ''; |
| … |
… |
class WP_Query {
|
| 1603 | 1604 | case 'meta_value_num': |
| 1604 | 1605 | $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0"; |
| 1605 | 1606 | break; |
| | 1607 | case 'post__in': |
| | 1608 | if ( ! empty( $this->query_vars['post__in'] ) ) { |
| | 1609 | $orderby_clause = "FIELD({$wpdb->posts}.ID," . implode( ',', array_map( 'absint', $this->query_vars['post__in'] ) ) . ')'; |
| | 1610 | } |
| | 1611 | break; |
| 1606 | 1612 | default: |
| 1607 | 1613 | if ( array_key_exists( $orderby, $meta_clauses ) ) { |
| 1608 | 1614 | // $orderby corresponds to a meta_query clause. |
| … |
… |
class WP_Query {
|
| 2252 | 2258 | } |
| 2253 | 2259 | } elseif ( 'none' == $q['orderby'] ) { |
| 2254 | 2260 | $orderby = ''; |
| 2255 | | } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) { |
| 2256 | | $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )"; |
| 2257 | 2261 | } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) { |
| 2258 | 2262 | $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )"; |
| 2259 | 2263 | } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) { |
diff --git tests/phpunit/tests/post/query.php tests/phpunit/tests/post/query.php
index 3e95e73605..1112c87e04 100644
|
|
|
class Tests_Post_Query extends WP_UnitTestCase {
|
| 174 | 174 | $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) ); |
| 175 | 175 | } |
| 176 | 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 | ) ); |
| | 206 | $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) ); |
| | 207 | } |
| | 208 | |
| 177 | 209 | function test_post__in_attachment_ordering() { |
| 178 | 210 | $post_id = self::factory()->post->create(); |
| 179 | 211 | $att_ids = array(); |