I have a page, outside the loop, that display a media attachment with some basic info about it (title, uploaded date, etc). I have the attachment ID available to me via a form submitted before the page loads.
What I need to do is secure the page by making sure that only users who have uploaded that file have access to the page with that file one it. In order to do that, I assumed the easiest way would be to compare the ID of the current, logged-in user with the ID of the user who created the attachment. But there doesn’t seems to be a way that I can find to get the ID of the author of a post outside of the loop using only the attachment ID.
I’m stumped, how should I do this?
Back in the day I wrote a function to check for that (you can put it in functions.php and call it from your page):
Also, theoretically you could just get the post object and compare the author’s ID with the current logged in user’s ID. Disclaimer: code below is untested.