diff --git src/wp-includes/class-wp-query.php src/wp-includes/class-wp-query.php
index baefec75b4..4da3a80768 100644
|
|
class WP_Query { |
1508 | 1508 | $allowed_keys = array( |
1509 | 1509 | 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified', |
1510 | 1510 | 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified', |
1511 | | 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand', |
| 1511 | 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand', 'post__in', |
1512 | 1512 | ); |
1513 | 1513 | |
1514 | 1514 | $primary_meta_key = ''; |
… |
… |
class WP_Query { |
1566 | 1566 | case 'meta_value_num': |
1567 | 1567 | $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0"; |
1568 | 1568 | break; |
| 1569 | case 'post__in': |
| 1570 | if ( ! empty( $this->query_vars['post__in'] ) ) { |
| 1571 | $orderby_clause = "FIELD({$wpdb->posts}.ID," . implode( ',', array_map( 'absint', $this->query_vars['post__in'] ) ) . ')'; |
| 1572 | } |
| 1573 | break; |
1569 | 1574 | default: |
1570 | 1575 | if ( array_key_exists( $orderby, $meta_clauses ) ) { |
1571 | 1576 | // $orderby corresponds to a meta_query clause. |
… |
… |
class WP_Query { |
2167 | 2172 | } |
2168 | 2173 | } elseif ( 'none' == $q['orderby'] ) { |
2169 | 2174 | $orderby = ''; |
2170 | | } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) { |
2171 | | $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )"; |
2172 | 2175 | } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) { |
2173 | 2176 | $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )"; |
2174 | 2177 | } 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 6ae8114607..3e87379c4f 100644
|
|
class Tests_Post_Query extends WP_UnitTestCase { |
139 | 139 | $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) ); |
140 | 140 | } |
141 | 141 | |
| 142 | /** |
| 143 | * @ticket 38034 |
| 144 | */ |
| 145 | public function test_orderby_post__in_array() { |
| 146 | $posts = self::factory()->post->create_many( 4 ); |
| 147 | |
| 148 | $ordered = array( $posts[2], $posts[0], $posts[3] ); |
| 149 | |
| 150 | $q = new WP_Query( array( |
| 151 | 'post_type' => 'any', |
| 152 | 'post__in' => $ordered, |
| 153 | 'orderby' => array( 'post__in' => 'ASC' ), |
| 154 | ) ); |
| 155 | $this->assertSame( $ordered, wp_list_pluck( $q->posts, 'ID' ) ); |
| 156 | } |
| 157 | |
142 | 158 | function test_post__in_attachment_ordering() { |
143 | 159 | $post_id = self::factory()->post->create(); |
144 | 160 | $att_ids = array(); |