Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WP Rest API vs - how can I get author information from v2 - the author ID?

I'm building a front end using WP JSON, and I need (among other things) the name of the author of each post.

In V1, this was easy - here's a fragment of a typical post object from calling http://example.com/wp-json/posts/1:

 {
    "ID": 1,
    "title": "Hello world!",
    "status": "publish",
    "type": "post",
    "author": {
        "ID": 1,
        "name": "admin",
        "slug": "admin",
        "URL": "",
        "avatar": "http:\/\/0.gravatar.com\/avatar\/c57c8945079831fa3c19caef02e44614&d=404&r=G",
        "meta": {
            "links": {
                "self": "http:\/\/example.com\/wp-json\/users\/1",
                "archives": "http:\/\/example.com\/wp-json\/users\/1\/posts"
            }
        }
    },

But in V2, for author, all we get back is the ID (an integer). Passing this back to the API, we get a list of every article an author has written - but how can I get the author's information - their name, avatar, etc.?

PS: boy the V2 documentation is sparse...V1 was much better...

like image 741
B. Kold Avatar asked Mar 22 '16 00:03

B. Kold


People also ask

How do I get data from API in WordPress?

If you want to use the Fetch API with WordPress, you simply have to call the fetch function in your JavaScript code. Follow that function with a . then handler to access the content. You can then display it on your website or in your web application.

What is WP REST API?

The WordPress REST API provides REST endpoints (URLs) representing the posts, pages, taxonomies, and other built-in WordPress data types. Your application can send and receive JSON data to these endpoints to query, modify and create content on your site.

How do I find my WordPress API posts?

To use the WordPress REST API, simply add /wp-json/wp/v2/posts to the end of your WordPress site URL. This will give you a list of posts (in JSON format). The default number of posts returned is 10, but you can choose to show more or less with the per_page argument — we'll talk about that below.


2 Answers

Append the query parameter _embed in your API URL

Embedding is triggered by setting the _embed query parameter on the request. This will then include embedded resources under the _embedded key adjacent to the _links key in JSON file... as showed in http://v2.wp-api.org/reference/links.html

Examples:

http://demo.wp-api.org/wp-json/wp/v2/posts?_embed

http://demo.wp-api.org/wp-json/wp/v2/posts?filter%5Bposts_per_page%5D=10&page=1&_embed

Getting Author Name from JSON, and showing:

{{postItem._embedded.author[0].name}}

Getting Featured Image:

{{postItem._embedded['wp:featuredmedia'][0].source_url}}

Categories:

{{postItem._embedded['wp:term'][0][0].slug}}
{{postItem._embedded['wp:term'][0][1].slug}}
{{postItem._embedded['wp:term'][0][2].slug}}
{{postItem._embedded['wp:term'][0][3].slug}}
...

and Tags:

{{postItem._embedded['wp:term'][1][1].slug}}
{{postItem._embedded['wp:term'][1][2].slug}}
...
like image 155
Fabiano Albernaz Avatar answered Sep 19 '22 08:09

Fabiano Albernaz


I think what you want is /wp-json/wp/v2/users/1

You should also find the direct url at _links > author > href

enter image description here

like image 27
Tom Woodward Avatar answered Sep 19 '22 08:09

Tom Woodward