Skip to content

Abilities API: Add the core/media-query ability - #16138

Open
jorgefilipecosta wants to merge 1 commit into
WordPress:trunkfrom
jorgefilipecosta:add/media-abilities
Open

jorgefilipecosta wants to merge 1 commit into
WordPress:trunkfrom
jorgefilipecosta:add/media-abilities

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Shape from WordPress/abilities#22. Same approach as #16028.

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

This PR adds the core/media-query ability to the content category, for attachments. The core/media-upload, core/media-update and core/media-delete abilities follow in #16408, stacked on this one.

  • core/media-query takes { where, fields, pagination } and returns { items, pagination } with the totals. ids, slugs, status, media_type, mime_type, post and author filter the attachments.
  • The ability requires a logged-in user and returns only the attachments the user can read.

Outside the new WP_Abilities_Media class:

  • Attachments get show_in_abilities, and core/content-query leaves them out.
  • wp_register_core_abilities() loads the class and registers the ability.

Querying the images, two per page:

await wp.apiFetch( {
	path: wp.url.addQueryArgs( '/wp-abilities/v1/abilities/core/media-query/run', {
		input: {
			where: { media_type: [ 'image' ] },
			fields: [ 'title.rendered', 'mime_type', 'media_details.width', 'media_details.height' ],
			pagination: { per_page: 2 },
		},
	} ),
} );
{
	"items": [
		{
			"id": 10,
			"title": { "rendered": "WordPress simplified logotype" },
			"mime_type": "image/png",
			"media_details": { "width": 500, "height": 500 }
		},
		{
			"id": 9,
			"title": { "rendered": "WordPress logotype with the mark" },
			"mime_type": "image/png",
			"media_details": { "width": 1000, "height": 1000 }
		}
	],
	"pagination": { "total": 3, "total_pages": 2 }
}

Testing

Verify unit tests are passing:

npm run test:php -- --group 66719

Or run the example above in the editor console as an administrator, on a site with a few images.

AI disclosure: the change and tests were drafted with AI assistance (Claude Code).

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

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jorgefilipecosta, euvoor, gziolo.

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@jorgefilipecosta

Copy link
Copy Markdown
Member Author

Known bug: for a PDF, the full size reports application/pdf instead of the image/jpeg type of its preview image. The REST API has the same bug, tracked in https://core.trac.wordpress.org/ticket/66811 and fixed in #16217.

@euvoor euvoor left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@jorgefilipecosta I tried the query-only split Grzegorz suggested on 8ac3b4e. With upload, update, delete and the download helper removed, all 99 media-query tests passed on single-site and multisite.

I'd keep the attachment show_in_abilities flag and the matching exclusion in core/content-query together in the read PR. One easy piece to miss is that format_media() calls the title, caption and description renderers by name. The query needs all three.

I also tried listing one post's three JPEGs, two per page, with a fourth JPEG left unattached. With distinct upload dates, both the ability and /wp/v2/media returned the same IDs on both pages and a total of three. When all three dates matched, the ability used descending ID to break the tie. REST returned the same images in a different order in my test. I'd keep the ability's tie-breaker for predictable pagination.

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Coding agents flagged two places where the media query response may differ from the design. I don’t know WordPress media handling well enough to judge either finding myself. Could you check the examples below and clarify what callers should expect before we merge?

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I’d like to request changes so we can land core/media-query first and review the write abilities in a follow-up, following the split @euvoor tested. I’ve left comments inline on the query behavior and the response fields the abilities share.

Once the split is ready, could you rerun the nine failing multisite CI jobs and investigate any failures that remain?

@jorgefilipecosta

Copy link
Copy Markdown
Member Author

Known bug: for a PDF, the full size reports application/pdf instead of the image/jpeg type of its preview image.

This was fixed in the PR, the full size of a PDF now reports the MIME type of its preview image 👍

Adds an ability for attachments.

`core/media-query` returns a page of attachments with the totals, filtered by IDs, slugs, status, media type, MIME type, parent post or author. It requires a logged-in user and returns only the attachments the user can read.

Attachments are exposed to the Abilities API through `show_in_abilities`, and `core/content-query` leaves them out.

See #66719.
@jorgefilipecosta jorgefilipecosta changed the title Abilities API: Add the core/media-query, core/media-upload, core/media-update and core/media-delete abilities Abilities API: Add the core/media-query ability Dec 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core abilities Core abilities related tasks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants