Fetch media by ID

Hi I have added custom field in post entries that will record the IDs of the uploaded media.
from the JSON it is know that the list of media is stored in

wp-json/wp/v2/media?parent=[postID]

How should I fetch the media by ID, or convert to a proper fetch URL?

after that, should retrieve them with state.source.attachment[id]?
or any other more preferred methods?
thanks!

Hi @kvhk

Welcome to the forum. You can use the value stored at state.source.attachment[id].link.

Hope this helps.

1 Like

i did not able to get the media data by state.source.attachment[id].link . as the media has not been fetched.

I solve the complication by using pods framework plugin to get all the media data. thanks!

Hello everyone, I am having this same issue. I fetch all data for my categories using the beforeSSR hook and I get access to all the information but the media link. And the media link (featured image) is not on the state.

Hi again.

I am back answering my own question. What I finally did was to add the featured image source in the rest object by using the register_rest_field WordPress function.

In the WordPress documentation there is an example on how to add the featured image source here: http://developer.wordpress.org/reference/functions/register_rest_field/

2 Likes