Skip to content

Conversation

@mikachan
Copy link
Member

@mikachan mikachan commented Nov 27, 2025

What?

In #73029, we introduced array as a media_type. This is a breaking change, as previously these were only allowed as a string, breaking any existing implementations. This PR allows both string and array for media_type.

Do we need to make any changes elsewhere to support this? Also, I assume this will need to be backported to WP 6.9.

Why?

Allow backwards compatibility.

How?

Allow for both arrays and strings.

Testing Instructions

@mikachan mikachan added [Type] Bug An existing feature does not function as intended [Feature] Media Anything that impacts the experience of managing media Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release labels Nov 27, 2025
@mikachan mikachan requested review from tellthemachines and removed request for spacedmonkey November 27, 2025 16:39
@github-actions
Copy link

github-actions bot commented Nov 27, 2025

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: mikachan <[email protected]>
Co-authored-by: ramonjd <[email protected]>
Co-authored-by: andrewserong <[email protected]>
Co-authored-by: t-hamano <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@ramonjd
Copy link
Member

ramonjd commented Nov 27, 2025

I've been testing Gutenberg and WP and am unsure how to replicate the original bug:

WP 6.8 + Gutenberg branch wp/6.9 (21.9.0)

Note: WP 6.8 doesn't support array types. Only string values work. Array return 400 (expected)

await wp.data.resolveSelect( 'core' ).getEntityRecords( 'postType', 'attachment', { media_type: 'image', per_page: 5 } );

Triggers the API call:

/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&context=edit&media_type=image&per_page=5&_locale=user

With the results

Screenshot 2025-11-28 at 8 39 55 am

Arrays are not supported in WP 6.9. The following is a 400:

await wp.data.resolveSelect( 'core' ).getEntityRecords( 'postType', 'attachment', { media_type: ['image', 'audio'], per_page: 5 } );

WP 6.8 + This Gutenberg branch fix/media-types-fix

Works as above. Strings but not arrays.

WP 6.8 + Gutenberg trunk

Both strings and arrays are accepted.

Filtering media_type by string

await wp.data.resolveSelect( 'core' ).getEntityRecords( 'postType', 'attachment', { media_type: 'image', per_page: 5 } );

Triggers the API call:

/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&context=edit&media_type=image&per_page=5&_locale=user

Filtering media_type by array

For example:

await wp.data.resolveSelect( 'core' ).getEntityRecords( 'postType', 'attachment', { media_type: 'image,audio', per_page: 5 } );
await wp.data.resolveSelect( 'core' ).getEntityRecords( 'postType', 'attachment', { media_type: ['image'], per_page: 5 } );
await wp.data.resolveSelect( 'core' ).getEntityRecords( 'postType', 'attachment', { media_type: ['image', 'audio'], per_page: 5 } );

Triggers an API call

/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&context=edit&media_type%5B0%5D=image&media_type%5B1%5D=audio&per_page=5&_locale=user

WP 6.9 (Gutenberg deactivated)

All work as above

WP 6.9 + Gutenberg trunk

All work as above

WP 6.9 + Gutenberg branch wp/6.9 (21.9.0)

All work as above

WP 6.9 + This Gutenberg branch fix/media-types-fix

All work as above

@ramonjd ramonjd added the No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core label Nov 27, 2025
@ramonjd
Copy link
Member

ramonjd commented Nov 27, 2025

What I can see is that #73029 was never backported to Gutenberg wp/6.9

https://github.com/WordPress/gutenberg/tree/wp/6.9/lib/compat/wordpress-6.9

But that only has a bearing on whether arrays AND strings are accepted for media_type.

If a site is running WordPress 6.8 then strings are fine.

@ramonjd
Copy link
Member

ramonjd commented Nov 27, 2025

What I can see is that #73029 was never backported to Gutenberg wp/6.9

BTW we should do this cc @tellthemachines ?

$params['media_type'] = array(
'default' => null,
'description' => __( 'Limit result set to attachments of a particular media type or media types.' ),
'type' => 'array',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See this convo over in the Core patch: WordPress/wordpress-develop#10054 (comment)

Incoming strings will be coerced to arrays. This code matches Core so it should be okay.

Did you manage to reproduce the bug anywhere in Core?

$params['mime_type'] = array(
'default' => null,
'description' => __( 'Limit result set to attachments of a particular MIME type or MIME types.' ),
'type' => 'array',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@ramonjd
Copy link
Member

ramonjd commented Nov 27, 2025

Thanks @mikachan for getting this PR up so quickly ❤️

Did you manage to reproduce the bug in Core? If so, could you share the testing steps. I'm struggling to reproduce

What I can see is that #73029 was never backported to Gutenberg wp/6.9

Withdraw that please! It was only PHP files and nothing to do with packages.

@andrewserong
Copy link
Contributor

Did you manage to reproduce the bug in Core? If so, could you share the testing steps. I'm struggling to reproduce

I'm unable to reproduce either. At least in theory, we shouldn't need to check whether it's a string, as the schema should be handling the coercion to an array. I.e. in core, for an array in the schema, rest_validate_array_value_from_schema handles sanitizing the value, which calls rest_sanitize_array, which handles wrapping / parsing into a list via wp_parse_list.... so by the time we're in prepare_items_query, I believe $request['media_type'], if it exists, should already be an array if the original request had media_type as a string (or comma separated list of values)

@mikachan
Copy link
Member Author

Thanks so much @ramonjd and @andrewserong for testing this and reporting back in such detail. I'm also struggling to reproduce this bug.

At least in theory, we shouldn't need to check whether it's a string, as the schema should be handling the coercion to an array

Based on this, I don't think we need this PR at all. Sorry for the false alarm!

@t-hamano
Copy link
Contributor

t-hamano commented Dec 1, 2025

Based on this, I don't think we need this PR at all. Sorry for the false alarm!

Can we close this PR?

@mikachan mikachan closed this Dec 1, 2025
@mikachan mikachan removed the Backport to Gutenberg Minor Release Pull request that needs to be backported to a Gutenberg minor release label Dec 1, 2025
@tyxla tyxla deleted the fix/media-types-fix branch December 1, 2025 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Media Anything that impacts the experience of managing media No Core Sync Required Indicates that any changes do not need to be synced to WordPress Core [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants