Attaching a file to a page without adding to media library?

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.