Skip to content

Abilities API: Replace the author_slug filter of core/content-query with an author object - #14673

Open
jorgefilipecosta wants to merge 3 commits into
WordPress:trunkfrom
jorgefilipecosta:update/content-query-author-object
Open

jorgefilipecosta wants to merge 3 commits into
WordPress:trunkfrom
jorgefilipecosta:update/content-query-author-object

Conversation

@jorgefilipecosta

@jorgefilipecosta jorgefilipecosta commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Follow up to https://core.trac.wordpress.org/changeset/64388. Builds on #14643, whose commit is included here until it lands.

What

  • The where.author_slug filter of core/content-query is replaced by a where.author object with the optional properties id and slug, the keys the author field of a post returns.
  • An ID or a slug that names no user matches no post and gives the empty result, as an unknown slug already does, and so do an ID and a slug that name different users.
  • 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 non-empty string are rejected as invalid filters, with where[author], where[author][id] and where[author][slug] as the parameter.

Why

How

  • The input schema describes author as an object with additionalProperties: false, minProperties: 1, id (integer, minimum 1) and slug (string, minLength 1), in the position author_slug had.
  • The execute callback checks the filter in order: post type support, object shape, id through the existing integer parsing (0 fails closed, since WP_Query reads author => 0 as no author filter), then slug looked up as given. The query argument stays a single author user ID.
  • Descriptions and docblocks that named author_slug now name author.
  • Tests: the existing author_slug tests 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:

wp.apiFetch( {
	path: '/wp-abilities/v1/abilities/core/content-query/run?input[where][type]=post&input[where][author][slug]=cq_author&input[fields][]=id&input[fields][]=title&input[fields][]=author',
} );

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:

npm run test:php -- --filter author

…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.
@github-actions

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. You can also manually ask me to refresh this list by adding the props-bot label.

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

Props jorgefilipecosta.

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

@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.

…`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant