How to get all the attachments in WordPress
- Article
- Comment
How to get all the attachments in WordPress. Getting all attachment is good one for the common operations with media files. Let’s try coding the feature to get all the attachments which attached with a post.
$args = array('post_type'=>'attachment','numberposts'=>null,'post_status'=>null); $attachments = get_posts($args); if($attachments){ foreach($attachments as $attachment){ // here your code echo $attachment->ID; } }
And if you want to a posts all attached images or medias, you can try the below code.
$args = array('post_type'=>'attachment','numberposts'=>null,'post_status'=>null, 'post_parent' => $post->ID); $attachments = get_posts($args); if($attachments){ foreach($attachments as $attachment){ // here your code echo $attachment->ID; } }
That’s it.