Im trying to find a way to pull random images from either a post or wordpress’s built in image library. Im not sure how to query image uris from a post or the media library – is there a tag or method that I can adopt? Should I just write something in PHP to manually query the wordpress database? If so what is the most efficient way to return image urls?
P.S. I would have posted this on WordPress Stack but it seemed more like a technical question than a wordpress-specific question so I brought here instead. WordPress stack questions have a bad habit of not getting answered.
WordPress has built-in functions to access the array on images attached to a post. I found this function on the web, and it might be just what you’re looking for. It fetches the images attached to a post, sorts them randomly, and returns the URL of the first one:
source