1

I am looking for a way to get all PDF Files from a WordPress post using the WordPress API. I get all attached files with this:

https://www.domain.com/wp-json/wp/v2/media?parent=1267

But when I try to get only PDF files with the following it does not work:

https://www.domain.com/wp-json/wp/v2/media?parent=1267&search=.pdf

Any ideas?

Best regards

1

You would get the PDFs by querying

mime_type=application/pdf

So, based on your example your full URL would be:

https://www.domain.com/wp-json/wp/v2/media?parent=1267&mime_type=application/pdf

However, it is possible when using the parent=ID parameter, you will see ALL the PDFs, not just the attachments specific to that post. This happens when the PDFs were uploaded to the media library, but not necessarily to a particular page/post.

Perhaps they were linked manually in the page/post, rather than being uploaded and inserted via Add Media or a Gutenberg block. You may also see nothing at all if the post doesn't have a PDF attached but all the other PDFs in the library have an attachment page assigned.

The query does work as you require when the files are specifically attached to a page/post.

To better explain what I mean, go to your media library and check the "Attachment Details" of one of your PDFs by clicking on it. If it does not include the line "Uploaded to", then it will show up on all queries to the PDF file type.

However, if it looks like the following, you should get the proper results with the query provided above showing only the PDFs attached to a specific page. WP PDF Attachment Details Basically, if the files do not have a specific upload path, the query will show all since it has no way to filter out the stray attachments.

| improve this answer | |
  • Hello, thanks for your answer. Both examples are not working for me. Both show no results? – tom84 Dec 11 '19 at 10:00
  • i tryed again... your first solution shows all pdfs, not only these from a certain url. Your second solution shows no result – tom84 Dec 11 '19 at 10:08
  • 1
    You were right, the second solution ended up not being correct. I removed it and edited the answer to explain why you were seeing ALL PDFs with the first solution. Hope this makes sense. The solution will show all the PDFs if they are not specifically uploaded to a particular page/post. If they are, specific to that post, it should work for you as you need it to. I tested this and fixed this issue on my own site as well. You may have to re-upload the PDFs to your post to get it working correctly. – Anthony D Dec 11 '19 at 15:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.