Jester
Jester

Reputation: 3317

Wordpress WP-API get images attached to post

so I am trying to use WP-API plugin:

i figured out how to get all posts for custom post type:

http://domain/wp-json/posts?type[]=customType

and i figured out how to get attachments:

http://domain/wp-json/media/

Now the problem is how to get images attached to single post. if I look at the soruce it seems to suggest a path like:

http://domain/wp-json/media/<postId>/

but I get an error:

[{"code":"json_post_invalid_type","message":"Invalid post type"}]

any Idea how to get this to work?

Upvotes: 1

Views: 6563

Answers (3)

Stonetip
Stonetip

Reputation: 1178

You can use the "parent" id argument, e.g:

$ curl http://demo.wp-api.org/wp-json/wp/v2/media?parent=<id>

where is the post's ID. What we do is ignore all versions of images except the original, which is found by looking for the GUID > rendered value, e.g:

guid: {
    rendered: "http://demo.wp-api.org/wp-content/uploads/image-belonging-to-this-post.png"
}

Upvotes: 3

Purus
Purus

Reputation: 5799

Just an update to this old post. WP-API now supports getting media related to a particular post.

http://v2.wp-api.org/reference/media/

Code:

$ curl http://demo.wp-api.org/wp-json/wp/v2/media/<id>

Upvotes: -1

Jester
Jester

Reputation: 3317

As of this moment this functionality does not seem to be supported probably will be when the plugin goes into core but for now it is as is.

I ended up making my own json API to serve my needs by making the page templates recive emit JSON data.

Upvotes: 0

Related Questions