Make WordPress Core

Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#64046 closed enhancement (duplicate)

Attachments REST API: Add support for filtering attachments by multiple media types

Reported by: ramonopoly's profile ramonopoly Owned by:
Milestone: Priority: normal
Severity: normal Version: 6.9
Component: REST API Keywords: has-patch has-unit-tests
Focuses: rest-api Cc:

Description (last modified by ramonopoly)

The WP REST Attachments Controller permits filtering by "media_type" in the query args.

A "media type" might be "image" or "video" or "audio".

But you can only filter by one type at a time.

What if I wanted all my audio and video files in the response.

Attachments Controller says no.

But if the "media_type" parameter also accepted an array of media types, users could filter attachment queries by more than one media types.

Luckily post_mime_type already supports an array of strings.

https://github.com/WordPress/wordpress-develop/blob/2446d399945bb6b8378b49c1c5b66197c323f0e3/src/wp-includes/class-wp-query.php#L2468

wp_post_mime_type_where

https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/post.php#L3676

Change History (10)

This ticket was mentioned in PR #10054 on WordPress/wordpress-develop by @ramonopoly.


2 months ago
#1

WIP - not ready for review

This update introduces a new media_types parameter to the WP REST Attachments Controller, allowing users to filter attachment queries by an array of media types. Additionally, the existing media_type parameter remains functional, ensuring backward compatibility. post_mime_type already supports multiple mime types.

cc @andrewserong

Trac ticket: https://core.trac.wordpress.org/ticket/64046

### Testing

npm run test:php -- --filter WP_Test_REST_Attachments_Controller

@swissspidy commented on PR #10054:


2 months ago
#2

I left a comment, but I'm wondering if we could consolidate the behaviour to the existing media_type arg, but set it to allow either a string or an array, rather than adding the extra arg?

Strongly suggest the same. No need for a new param.

#3 @ramonopoly
2 months ago

  • Description modified (diff)

@ramonopoly commented on PR #10054:


2 months ago
#4

I completely missed https://core.trac.wordpress.org/ticket/63668 and https://github.com/WordPress/wordpress-develop/pull/9211

Looks like https://github.com/WordPress/wordpress-develop/pull/9211 handles mime_type as well.

I'll ping over there to see if work can carry on in that PR. If, for any reason, it cannot, I'll bring the changes over here and assign props to @himanshupathak95

#5 @ramonopoly
2 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #63668.

@ramonopoly commented on PR #10054:


2 months ago
#6

I've based this PR on work over in https://github.com/WordPress/wordpress-develop/pull/9211

I think it's ready for another look - the base functionality hasn't changed, except for the additional feature of being able to filter by multiple mime types AND multiple media types or a combination of both.

@andrewserong commented on PR #10054:


2 months ago
#7

This is still testing well for me 👍
Just left a question about whether or not we should be including all the mime types in an enum or go with a simple string type in the collection params, but other than that it's looking good to me!

@andrewserong commented on PR #10054:


2 months ago
#8

This is still looking good to me after the latest changes 👍

@ramonopoly commented on PR #10054:


2 months ago
#9

Thanks @andrewserong! I'll let it settle a day or two then merge in it.

@ramonopoly commented on PR #10054:


2 months ago
#10

Committed in r60917

Note: See TracTickets for help on using tickets.