How To Specify the fields to Wordpress API v2
Asked Answered
O

4

16

I'm using the WordPress REST API in my project and sending a GET request to:

http://myblog/wp-json/wp/v2/posts  

and It's working quite alright but I want to specify the fields though I don't know how. I have looked at the documentation and still don't know how to go about it. For example, using the public API:

https://public-api.wordpress.com/rest/v1.1/sites/www.mysite.com/posts?number=100&fields=title,excerpt,featured_image 

returns only the specified fields. How do I this with the v2 API?

Outboard answered 31/3, 2016 at 17:15 Comment(3)
v2.wp-api.org/extending/modifyingClarkclarke
What don't you get? The REST API v2 returns a certain set of default fields, and if you want different ones, then you have to implement this as described in that document.Clarkclarke
Don't have access to the client's CP. I was hoping I could do that straight from the URL.Outboard
P
30

Here's how to access the a list of titles and excerpts using REST API v2:

https://www.example.com/wp-json/wp/v2/posts?_fields[]=title&_fields[]=excerpt&per_page=100&offset=100
Principe answered 26/7, 2020 at 16:1 Comment(5)
ReferenceEuphorbia
It also accepts request like this: example.com/wp-json/wp/v2/…Burushaski
@Hardik, thanks for the great shortcut there!Kwasi
update: it works with v3 tooYamauchi
God that documentation is trash. Where are the list of fields you can query?Population
C
2

https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/

As it states there, the REST API v2 returns a certain set of default fields, and if you want different ones, then you have to implement this as described in that document.

Clarkclarke answered 1/4, 2016 at 22:56 Comment(0)
V
1

The easy solution to this issue would be to use the ACF to REST API or an equivalent plugin that can extend the REST API for you. I have used this plugin on many sites successfully.

If this is not possible then you will need to modify the response as has been outlined by other answers. You can read more about that here.

Veal answered 22/7, 2016 at 17:28 Comment(0)
H
-1

You can use ?_fields[]=title&_fields[]=excerpt

Hand answered 16/11, 2020 at 8:5 Comment(1)
Isn't it already the solution given by @mattmaldre ?Triage

© 2022 - 2024 — McMap. All rights reserved.