[wp-trac] [WordPress Trac] #64073: Attachments REST API: extend to support ordering by `mime_type`.

WordPress Trac noreply at wordpress.org
Fri Oct 3 23:13:59 UTC 2025


#64073: Attachments REST API: extend to support ordering by `mime_type`.
--------------------------------------+------------------------------
 Reporter:  ramonopoly                |       Owner:  (none)
     Type:  enhancement               |      Status:  new
 Priority:  normal                    |   Milestone:  Awaiting Review
Component:  REST API                  |     Version:  trunk
 Severity:  normal                    |  Resolution:
 Keywords:  has-patch has-unit-tests  |     Focuses:  rest-api
--------------------------------------+------------------------------

Comment (by ramonopoly):

 @TimothyBlynJacobs Thanks for the note here.



 Are these the ones you're referring to?

 See:

 https://core.trac.wordpress.org/ticket/47642

 https://core.trac.wordpress.org/ticket/52907

 Looks like it's related to a long-standing Core issue where posts share
 the non-unique fields as you say - I came across it working with
 `post_date`


 I think the solution has something to do with adding deterministic
 ordering, e.g., ordering also by ID

 #47642 has a patch, it's a bit old.

 Maybe a filter could work to avoid that blocker?


 {{{
 /*
  * Ensure deterministic ordering to prevent duplicate records across
 pages.
  * When multiple attachments have the same mime_type, add ID as secondary
 sort to guarantee consistent ordering.
  * This prevents the same attachment from appearing on multiple pages when
 filtering by media_type.
  */
 if ( empty( $query_args['orderby'] ) || 'post_mime_type' ===
 $query_args['orderby'] ) {
     $query_args['orderby'] = array(
         'post_mime_type' => $query_args['order'] ?? 'DESC',
         'ID'             => 'DESC',
     );
 }
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/64073#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list