Get Attached Media For A Certain Post With REST API

UPDATED 03/09/2017: According to the API documentation, we can now use endpoint "wp-json/wp/v2/media?parent=POST_ID" to get the attachments to a certain post without an extra filter. For example:

When trying to get attached media for a certain post in WordPress with JSON REST API, I thought it would be something like:


But it didn’t work. So I turned to to find the solution.

The documentation of retrieve posts and get attachments are not clear enough to show that I may or may not do so with REST API. Luckily I found the following paragraph in the Comparison section: API embeds all attachments in the post. WP API does not currently dothis. Attachments belonging to the post can still be queried via/media?filter[post_parent]=<id>.

I tried it right away but to my surprise, it didn’t work either.

After more googling I saw people posted couple of issues on the GitHub repo of REST API, which reported the same problem as mine.

I got an email this morning from a passionate AngularJS and WordPress learner, Stuart, who generously shared his finding to fix this issue. It turned out couple days after my googling, one of the contributors to REST API, Daniel closed several related issues with the solution: We need to add an extra filter as below to make the media query work.

add_filter( 'query_vars', function( $query_vars ) {
    $query_vars[] = 'post_parent';
    return $query_vars;

To be honest I feel this approach is a bit of redundant. The “post_parent” should be a standard query var rather than one we need to hook to enable. But considering to REST API is still a young project and we’re lucky to have so many great developers in WordPress community to get involved, I supposed there must be some reasons to do so.

Now you can go try and see if it works for you. In the following posts of my Building themes with AngularJS and JSON REST API series, I’ll be showing how to build a slider with Angular Slick in your WordPress post. So stay tuned!

10 responses

  1. […] time we just learned about Getting Attached Media For A Certain Post With REST API, now we should be ready for implementing this technique and fetching all attached images into a […]

  2. Lamari Aderrahim Avatar
    Lamari Aderrahim

    Nice tuto
    ty for sharing your experience with the world

    1. Yoren Chang Avatar
      Yoren Chang

      Thanks for stopping by. My pleasure. 🙂

  3. Where is the best place to put the “add_filter” statement? I haven’t done that with WordPress before, and I need this exact functionality from this API. SO thankful you published this page!

    1. Yoren Chang Avatar
      Yoren Chang

      Hi Wayne, `add_filter` should go to your `functions.php`.

  4. Hello Yoren

    It will help us immensely if you may please answer this question :: Should we use or ?

    At present we are using Using this plugin, we are not being able to display the “audio” and “video” associated with a post, in our Ionic app.

    Thanks a lot for your help!
    ~ Ari

  5. Yoren Chang Avatar
    Yoren Chang

    Hi Ari, I think both should work but the route / query vars might be different. I don’t have a good idea since I haven’t followed up the latest API plugin for a while. Do you mean you can get every attachment but neither audio nor video?

  6. Arslan Avatar

    Hi, Thank you for the post.

    Is it possible to have WordPress search, that allows filter the search results by media type?

    For example, look here:

    1. Yoren Chang Avatar
      Yoren Chang

      I don’t think we have it in WP API by default, should need some hooks to hack around.

  7. Hi Yoren,
    I’m trying to create a custom endpoint to send more than one media on the api request and return an array media json of the uploaded files. Do you know if is it possible?
    When I try this, $request->get_file_params() is always empty…

Leave a Reply

Your email address will not be published. Required fields are marked *