WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 5 weeks ago

#49985 assigned enhancement

REST API: Using _embed and _fields query parameters in the same query

Reported by: anouarbenothman Owned by: TimothyBlynJacobs
Milestone: 5.7 Priority: normal
Severity: normal Version: 5.4
Component: REST API Keywords: needs-patch needs-unit-tests early
Focuses: rest-api Cc:

Description

When performing a REST API request and adding the _embed and _fields query parameters. the API attempts to skip unneeded fields but does not embed resources that are linked with embeddable set to true.

In some cases we may need _embed and _fields in the same query.
I am requesting the feature to be added to allow the filtering of resources and embedding them (the embeddable ones) at the same time when specifying the _embed and _fields.

Example:
http://<site>/wp-json/wp/v2/posts?_fields=title,author&_embed=author

If implemented, the above request should only return title and author, and embed the author.

Change History (5)

#1 @ocean90
7 months ago

Hello @anouarbenothman, welcome to Trac!

Thanks for the report. I noticed this a while ago too but seems like I never created a ticket for it. The workaround is to include _links in the fields too so you get at least all embed data.

No embed: https://wordpress.org/news/wp-json/wp/v2/posts?_embed=true&_fields=id,title
With embed: https://wordpress.org/news/wp-json/wp/v2/posts?_embed=true&_fields=id,title,_links

#49538 is about getting only a subset of the embed data.

#2 @TimothyBlynJacobs
7 months ago

  • Keywords needs-patch needs-unit-tests added
  • Milestone changed from Awaiting Review to Future Release

Yeah this is a bit of an annoying issue. The problem is that rest_filter_response_fields will remove the _links field before link embedding is processed. So the server doesn't know what resources to embed.

We could perhaps fix this by not excluding _links in rest_filter_response_fields if _embed is set.

Another step would be to reduce the _links to only contain entries specified by _embed if _links is not included in _fields at all.

#3 @anouarbenothman
7 months ago

  • Keywords needs-patch needs-unit-tests removed

Thank you very much for the reply.

#4 @anouarbenothman
7 months ago

  • Keywords needs-patch needs-unit-tests added

#5 @TimothyBlynJacobs
5 weeks ago

  • Keywords early added
  • Milestone changed from Future Release to 5.7
  • Owner set to TimothyBlynJacobs
  • Status changed from new to assigned
Note: See TracTickets for help on using tickets.