You can use get_posts()
function to get random post, without additional php manipulation.
Just change default orderby attribute to “rand” and set number of posts attribute equals to “1”.
$image = get_posts(
array(
'orderby' => 'rand', //random order
'numberposts' => 1, // numberposts, not posts_per_page
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_status' => 'inherit'
)
);
//for testing purposes
echo $image[0]->ID;
Related Posts:
- Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails
- How can I bulk delete media and attachments using WP-CLI?
- complex restriction of items in media library
- Edit image / Delete image link
- media_sideload_image file name?
- Get all posts (of any post type) an attachment is used in
- Unattaching unlinked images
- Show Post ID in “Find Posts or Pages” box in Media Library?
- Gel all image from certain post type
- Can i hide the attachments from media which I uploaded from front end?
- Get the attachment URL on single.php
- Perform a custom (bulk) action in media view
- How to retrieve alt attribute for an attachment (uploaded image)?
- Where does the gallery shortcode gets its attachments from?
- Control attachments menu order with jQuery Sortable
- Is it possible to share media files between multiple WordPress sites?
- Open the Attachment Details popup
- Proper use of wp_get_attachment_image?
- Upload multiple image with media_handle_upload with multiple file field [duplicate]
- Get an image from the media library. Struggling with the code?
- Media Library Mess After FTP … /uploads
- Media item permalink goes to source file instead of attachment.php
- wp_get_attachment_image_src calling wrong location
- Removing images deleted from uploads folder, from Media Library
- WP media attachment location vs permalink
- Removing delete permanently button in uploading files media
- Retrieve ONLY mp3s and set a variable for use in a playlist called by single.php template?
- Is there a way to add Reattach and Unattach links in image gallery?
- How to retrieve images contained in a post
- How to save media-form on custom tab
- Bulk delete media by year
- Media library upload hook
- Can I add a Category Metabox to attachment?
- How to trigger a refresh in the media modal
- Get all post attachments except featured image
- Changing attachment urls?
- Add custom class to attachment in media library grid mode
- How can I UN-attach media from a post?
- Is it possible to query specific WordPress Attachment files (.ppt & .pdf) and output them in a list?
- Password protect some uploaded files, so only logged-in users can view them
- How to get attachments for a specific post type?
- How to add media from front-end to an existing post?
- Make individual attachments private?
- Change the Permalink for wordpress attachment
- How to get list of paths (not urls) for an image
- Get post embedded image caption
- How to count media attachments?
- Insert custom ID into wp_get_attachment_link
- How to add attachment without uploading?
- Reorder “menu order” of attachments to another predefined order
- Change code to display image attachment page
- Add image attachment to post via coding?
- Show selected images on top in Media Manager
- How To Grab All Type Of Attachments, But Images?
- Removing width and height attributes from wp-get-attachment
- Change filename during upload
- Get post attachment images dimension and use in embed code
- use wp_get_attachment_image() to show attachments
- List Post Attachments – Remove Image Thumbnails
- How do i add LIGHTBOX to Attachments?
- How to specify width and height in the_content for image attachments
- Post edit – Media Library – Only get images from current post
- delete image attachments of a custom post type except featured image
- Import Attachments – not attaching to post_parent
- How to redirect ‘file url’ to ‘post url’ i.e. in attachment.php
- hook wp_generate_attachment_metadata does not read all the attachments
- Massive photogallery import
- Delete associated thumbnails when calling wp_delete_attachment
- How to allow empty title for attachments?
- How to load attachment in media library for current user?
- Post Gallery list attachments except the one used as post thumbnail
- Echo Permalink in Attachment outside of loop?
- How can I display a link to a post’s (Word doc or pdf) attachment?
- GUID field in wp_posts – relevance for attachments?
- Is there a function for getting the post/page that an attachment is related to?
- Prevent image from being accessible via url as a post?
- get_post orderby not working in plugin
- Delete child post and attachment links to parent post
- Remove attachment from database
- Display Audio Attachment URL
- Display a post attachment depending on the caption
- changing the default picture size link url
- File Attachment display title in template
- Programmatically Altering Gallery Link and image size?
- File was uploaded, but it is not shown on media section of the dashboard
- wp_get_attachment_metadata is empty
- Attachments plugin cannot click media library button [closed]
- Restricted Attachments/Change Attachment Upload Location
- Hide Image Container if No Image Attachments
- How to link images to attachment.php [duplicate]
- Check whether attachment ID is associated with more than one post?
- Possible to “Attach” images to multiple posts without uploading twice from Add media Section?
- ID of images from gallery
- WordPress Attachments vs Post Meta
- open image after clicking on single.php with different stylesheet in wordpress
- How do I get link URL (not file URL or attachment URL) of an attached image [duplicate]
- Attachment page custom title?
- No attachments on contact form 7
- Using autocomplete with post_tag taxonomy on attachments
- wp_mail attachment not working in wordpress