Skip to content

Commit 77fb3eb

Browse files
committed
Updates WP_Query::parse_orderby to allow post_mime_type
1 parent c5df375 commit 77fb3eb

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
@@ -2707,22 +2707,44 @@ public function test_get_items_orderby_mime_type() {
27072707
)
27082708
);
27092709

2710+
$avif_id = self::factory()->attachment->create_object(
2711+
self::$test_avif_file,
2712+
0,
2713+
array(
2714+
'post_mime_type' => 'video/avif',
2715+
'post_excerpt' => 'A sample caption',
2716+
)
2717+
);
2718+
2719+
$svg_id = self::factory()->attachment->create_object(
2720+
self::$test_svg_file,
2721+
0,
2722+
array(
2723+
'post_mime_type' => 'image/svg+xml',
2724+
'post_excerpt' => 'A sample caption',
2725+
)
2726+
);
2727+
27102728
$request = new WP_REST_Request( 'GET', '/wp/v2/media' );
2729+
$request->set_param( '_fields', 'id,mime_type' );
27112730

27122731
// Check ordering. Default ORDER is DESC.
27132732
$request->set_param( 'orderby', 'mime_type' );
27142733
$response = rest_get_server()->dispatch( $request );
27152734
$data = $response->get_data();
2716-
$this->assertCount( 2, $data, 'Response count for orderby DESC mime_type is not 2' );
2717-
$this->assertSame( $png_id, $data[0]['id'], 'PNG ID not found in response for orderby DESC mime_type' );
2718-
$this->assertSame( $jpeg_id, $data[1]['id'], 'JPEG ID not found in response for orderby DESC mime_type' );
2735+
$this->assertCount( 4, $data, 'Response count for orderby DESC mime_type is not 4' );
2736+
// Check that ordering is working by verifying the mime types are in order
2737+
$mime_types = array_column( $data, 'mime_type' );
2738+
$expected_desc = array( 'video/avif', 'image/svg+xml', 'image/png', 'image/jpeg' );
2739+
$this->assertSame( $expected_desc, $mime_types, 'MIME types not in expected DESC order' );
27192740

27202741
// ASC order.
27212742
$request->set_param( 'order', 'asc' );
27222743
$response = rest_get_server()->dispatch( $request );
27232744
$data = $response->get_data();
2724-
$this->assertCount( 2, $data, 'Response count for orderby ASC mime_type is not 2' );
2725-
$this->assertSame( $jpeg_id, $data[0]['id'], 'JPEG ID not found in response for orderby ASC mime_type' );
2726-
$this->assertSame( $png_id, $data[1]['id'], 'PNG ID not found in response for orderby ASC mime_type' );
2745+
2746+
$mime_types = array_column( $data, 'mime_type' );
2747+
$expected_asc = array( 'image/jpeg', 'image/png', 'image/svg+xml', 'video/avif' );
2748+
$this->assertSame( $expected_asc, $mime_types, 'MIME types not in expected ASC order' );
27272749
}
27282750
}

0 commit comments

Comments
 (0)