Repository navigation
Abilities API: Replace the author_slug filter of core/content-query with an author object - #14673
jorgefilipecosta wants to merge 3 commits into
Conversation
…s no user. The `core/content-query` ability rejected an `author_slug` filter that names no user as an invalid filter, and looked the slug up against the users the current user may see, with a rule of its own. The other filters, such as `ids` and `slugs`, give the empty result when nothing matches their value. Treat `author_slug` the same way, to fix possible inconsistencies: a slug that names no user now matches no post, like a user without posts the current user may read, and the lookup of visible users is removed. A value that is not a string is still rejected. Follow-up to [64388]. See #66268.
…y` with an `author` object. The `core/content-query` ability filtered posts by author through a `where.author_slug` string, while each post returns its author as an `author` object with `id`, `name`, and `slug`. The REST API posts collection filters by author ID, which the ability did not take. Replace the filter with a `where.author` object that takes the `id` or the `slug` the `author` field returns, so a result's author can be given back as a filter. An ID needs no user lookup. When both are given they must name the same user. An ID or slug that names no user matches no post, as before for a slug. A value that is not an object with an ID or a slug, an ID that is not a positive integer, and a slug that is not a string are rejected as invalid filters. Follow-up to [64388]. See #66268.
|
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. |
…`core/content-query`. Say in the schema that an ID and a slug naming different users match no post, mention the filter in the output `author.id` description, and ask for a non-empty slug in the error message. Cover an empty-string `author`, a whole float ID, and which problem an author filter with several reports. Follow-up to [64388]. See #66268.
Follow up to https://core.trac.wordpress.org/changeset/64388. Builds on #14643, whose commit is included here until it lands.
What
where.author_slugfilter ofcore/content-queryis replaced by awhere.authorobject with the optional propertiesidandslug, the keys theauthorfield of a post returns.idor aslug, anidthat is not a positive integer, and aslugthat is not a non-empty string are rejected as invalid filters, withwhere[author],where[author][id]andwhere[author][slug]as the parameter.Why
author: { id, name, slug }, so a caller can now pass that object back as the filter instead of translating it to a differently named string. @gziolo asked for this shape in Abilities API: Return the empty result for anauthor_slugthat names no user #14643 (comment).How
authoras an object withadditionalProperties: false,minProperties: 1,id(integer, minimum 1) andslug(string, minLength 1), in the positionauthor_slughad.idthrough the existing integer parsing (0 fails closed, sinceWP_Queryreadsauthor => 0as no author filter), thensluglooked up as given. The query argument stays a singleauthoruser ID.author_slugnow nameauthor.author_slugtests are converted, with data providers for the invalid shapes, IDs and slugs, the ID-and-slug combinations, filtering by ID, and the schema shape.Example input, filtering the posts by their author's slug:
{ "where": { "type": "post", "author": { "slug": "cq_author" } }, "fields": [ "id", "title", "author" ] }The same author can be given by ID,
"author": { "id": 3 }, or by both,"author": { "id": 3, "slug": "cq_author" }. The run endpoint takes the input as query arguments:Example output:
{ "items": [ { "id": 7, "title": { "raw": "Ann published", "rendered": "Ann published" }, "author": { "id": 3, "name": "Ann Author", "slug": "cq_author" } } ], "pagination": { "total": 1, "total_pages": 1 } }An author that names no user,
"author": { "slug": "nobody-here" }, or an ID and a slug naming different users, gives the empty result:{ "items": [], "pagination": { "total": 0, "total_pages": 0 } }The change was drafted with AI assistance and reviewed by me.
Trac ticket: https://core.trac.wordpress.org/ticket/66268
Testing
Verify unit tests are passing: