B. Kold
B. Kold

Reputation: 169

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

Upvotes: 15

Views: 23448

Answers (2)

Fabiano Albernaz
Fabiano Albernaz

Reputation: 658

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

Upvotes: 37

Tom Woodward
Tom Woodward

Reputation: 1713

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

Upvotes: 11

Related Questions