-
Notifications
You must be signed in to change notification settings - Fork 3.1k
REST API: Extend orderby
parameter to support mime_type
for attachments.
#10135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: trunk
Are you sure you want to change the base?
REST API: Extend orderby
parameter to support mime_type
for attachments.
#10135
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
'post_date', | ||
'post_title', | ||
'post_modified', | ||
'post_mime_type', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before I add tests (in Tests_Query_Results I think(, I'd like to check if adding post_mime_type
to this whitelist is okay?
The alternative is to add a filter in the attachments controller I guess, e.g.,
// In prepare_items_query method, after the orderby mapping:
if ( isset( $query_args['orderby'] ) && isset( $request['orderby'] ) && 'mime_type' === $request['orderby'] ) {
// Use posts_orderby filter to add custom ordering
add_filter( 'posts_orderby', array( $this, 'orderby_mime_type' ), 10, 2 );
// Set orderby to something that won't conflict
$query_args['orderby'] = 'date';
}
public function orderby_mime_type( $orderby, $query ) {
global $wpdb;
$order = $query->get( 'order' );
return "{$wpdb->posts}.post_mime_type {$order}";
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TimothyBJacobs made a good point about a long-standing pagination bug
This change might make that bug more obvious, not convinced it's a blocker thought.
It does make me lean into using a filter in the controller thought as we can set a deterministic field like id
to get around the pagination bug.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before I add tests (in Tests_Query_Results I think(, I'd like to check if adding post_mime_type to this whitelist is okay?
IMO because post_mime_type
is a field on the wp_posts
table, I think including it in this allow list makes sense, rather than adding a filter to the attachments controller. But I'm not very experienced with the WP_Query class, so feel free to get a second opinion!
made a good point about a long-standing pagination bug
That is a good point. I think it's a really important bug to fix as there's going to be lots of situations where it'll crop up (i.e. order by post_author
). But as you mention, this issue exists already, and there are lots of other fields where this situation can come up, so I wouldn't think of it as a blocker to this particular PR, either.
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
Outdated
Show resolved
Hide resolved
src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
Outdated
Show resolved
Hide resolved
…hments. This update allows the `orderby` request argument in the attachments controller to include `mime_type`, enhancing the sorting capabilities of media items. Additionally, a new test has been added to verify the correct functionality of this feature. Fixes trac 64073.
combine isset Co-authored-by: Timothy Jacobs <timothy@ironbounddesigns.com>
period Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
5369643
to
0800c1d
Compare
This update allows the
orderby
request argument in the attachments controller to includemime_type
, enhancing the sorting capabilities of media items.Trac ticket: https://core.trac.wordpress.org/ticket/64073
Testing
npm run test:php -- --filter WP_Test_REST_Attachments_Controller
In the block editor: