Skip to content

Commit 98fd52b

Browse files
committed
Updates WP_Query::parse_orderby to allow post_mime_type
1 parent 5203dbf commit 98fd52b

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/wp-includes/class-wp-query.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,7 @@ protected function parse_search_order( &$q ) {
16791679
* Converts the given orderby alias (if allowed) to a properly-prefixed value.
16801680
*
16811681
* @since 4.0.0
1682+
* @since 6.9.0 Extends allowed_keys to support ordering by `post_mime_type`.
16821683
*
16831684
* @global wpdb $wpdb WordPress database abstraction object.
16841685
*
@@ -1695,6 +1696,7 @@ protected function parse_orderby( $orderby ) {
16951696
'post_date',
16961697
'post_title',
16971698
'post_modified',
1699+
'post_mime_type',
16981700
'post_parent',
16991701
'post_type',
17001702
'name',
@@ -1748,6 +1750,7 @@ protected function parse_orderby( $orderby ) {
17481750
case 'post_author':
17491751
case 'post_date':
17501752
case 'post_title':
1753+
case 'post_mime_type':
17511754
case 'post_modified':
17521755
case 'post_parent':
17531756
case 'post_type':

src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function register_routes() {
7070
* prepares for WP_Query.
7171
*
7272
* @since 4.7.0
73+
* @since 6.9.0 Extends the `orderby` request argument to support `mime_type`.
7374
*
7475
* @param array $prepared_args Optional. Array of prepared arguments. Default empty array.
7576
* @param WP_REST_Request $request Optional. Request to prepare items for.
@@ -100,6 +101,17 @@ protected function prepare_items_query( $prepared_args = array(), $request = nul
100101
add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
101102
}
102103

104+
// Map to proper WP_Query orderby param - this needs to happen AFTER parent class
105+
if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) ) {
106+
$orderby_mappings = array(
107+
'mime_type' => 'post_mime_type',
108+
);
109+
110+
if ( isset( $orderby_mappings[ $request['orderby'] ] ) ) {
111+
$query_args['orderby'] = $orderby_mappings[ $request['orderby'] ];
112+
}
113+
}
114+
103115
return $query_args;
104116
}
105117

tests/phpunit/tests/rest-api/rest-attachments-controller.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2852,22 +2852,44 @@ public function test_get_items_orderby_mime_type() {
28522852
)
28532853
);
28542854

2855+
$avif_id = self::factory()->attachment->create_object(
2856+
self::$test_avif_file,
2857+
0,
2858+
array(
2859+
'post_mime_type' => 'video/avif',
2860+
'post_excerpt' => 'A sample caption',
2861+
)
2862+
);
2863+
2864+
$svg_id = self::factory()->attachment->create_object(
2865+
self::$test_svg_file,
2866+
0,
2867+
array(
2868+
'post_mime_type' => 'image/svg+xml',
2869+
'post_excerpt' => 'A sample caption',
2870+
)
2871+
);
2872+
28552873
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
2874+
$request->set_param( '_fields', 'id,mime_type' );
28562875

28572876
// Check ordering. Default ORDER is DESC.
28582877
$request->set_param( 'orderby', 'mime_type' );
28592878
$response = rest_get_server()->dispatch( $request );
28602879
$data = $response->get_data();
2861-
$this->assertCount( 2, $data, 'Response count for orderby DESC mime_type is not 2' );
2862-
$this->assertSame( $png_id, $data[0]['id'], 'PNG ID not found in response for orderby DESC mime_type' );
2863-
$this->assertSame( $jpeg_id, $data[1]['id'], 'JPEG ID not found in response for orderby DESC mime_type' );
2880+
$this->assertCount( 4, $data, 'Response count for orderby DESC mime_type is not 4' );
2881+
// Check that ordering is working by verifying the mime types are in order
2882+
$mime_types = array_column( $data, 'mime_type' );
2883+
$expected_desc = array( 'video/avif', 'image/svg+xml', 'image/png', 'image/jpeg' );
2884+
$this->assertSame( $expected_desc, $mime_types, 'MIME types not in expected DESC order' );
28642885

28652886
// ASC order.
28662887
$request->set_param( 'order', 'asc' );
28672888
$response = rest_get_server()->dispatch( $request );
28682889
$data = $response->get_data();
2869-
$this->assertCount( 2, $data, 'Response count for orderby ASC mime_type is not 2' );
2870-
$this->assertSame( $jpeg_id, $data[0]['id'], 'JPEG ID not found in response for orderby ASC mime_type' );
2871-
$this->assertSame( $png_id, $data[1]['id'], 'PNG ID not found in response for orderby ASC mime_type' );
2890+
2891+
$mime_types = array_column( $data, 'mime_type' );
2892+
$expected_asc = array( 'image/jpeg', 'image/png', 'image/svg+xml', 'video/avif' );
2893+
$this->assertSame( $expected_asc, $mime_types, 'MIME types not in expected ASC order' );
28722894
}
28732895
}

0 commit comments

Comments
 (0)