We are using WP for our new intranet, which will primarily be used for documentation and archiving. We have some pages where we want to attach PDFs, documents, images, etc. However, we do not want these media items to be added to the media library when we attach them to a page; i.e., if the page is deleted, the media items are also deleted. Is there a way to do this?


  • Media items shouldn't be deleted when a post or a page is deleted. If you're hosting them somewhere else you can just paste the URL/path the file. Jan 7, 2021 at 21:29

1 Answer 1


There is no such a solution out of the box, and any files uploaded to WP are publically available to anyone who can access the domain, so you might want to keep that in mind.

You'll need to code a solution, possibly as follows:

  • on post creation, use wp_insert_post to create the require folder.
  • Use something like glob($root_folder.$post_slug . "/*.{jpg,jpeg,png,pdf}",GLOB_BRACE) to list all files in the post-specific folder . (Note, if the slug is ever changed, you'll need to manually rename the folder or automate it somehow.
  • Use before_delete_post to delete the folder & all files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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