Search code examples
phpjsonwordpresswordpress-rest-api

how to get specific data from wp json rest api


i've installed wp-json-rest-api on my Wordpress website and i want to connect my website to an android app. In such a url, it gets all the posts:

http://mywebsite.com/wp-json/posts

On one of the android pages, i want to get ONLY title of posts, nothing more. I mean how should i specify in the url or anywhere else that i just want titles and IDs not all contents of posts? for example in page of each post, i just want to get title, id and content of that post, something like this, where id of the post is 12:

http://mywebsite.com/wp-json/posts?include=title,id,content/12

but in other pages like list of all posts, i want something like this:

http://mywebsite.com/wp-json/posts?include=title,id

what should i do?


Solution

  • One way to do this would be to create a custom endpoint. This way you can write your own functions and export their results via the REST API. In fact the REST documentation has a sample that does something like what you're asking. See here: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/ and just modify their sample to remove the author query logic and just return the list of titles!