I am working on a custom attachment.php template file for a theme and I want to make it link back to the document that the attachment is included within.

Naturally an attachment may be in multiple documents, but I just wondered if theres a built in or easy way to obtain a list of the posts?


There isn't really a built-in way to return all the posts that an attachment is used in. The closest thing would probably be $post->post_parent which all attachments have post parents.

There is a plugin called Find Posts Using Attachment that does a good job of retrieving these and is a single file PHP class so you could possibly repurpose it to obtain the desired result.

| improve this answer | |

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.