I'm new to WordPress theme development and I'm trying to figure out if there is a way to display a list of public attachments similar to how you can view a list of posts with a specific tag or author.

That is, if I want to retrieve a list of posts with a specific tag, I can submit something like '?tag=mytag' in the URL and Wordpress will spit out all the posts tagged with 'mytag'. So what is the equivalent of this for viewing attachments? Or is there one?

What I'm specifically wanting to do is allow the user to click a link and have a gallery of attachments (images, video, music, etc) display. I want attachments from ALL posts on the site, not just attachments of one specific post.

I've looked into the attachment.php file, but this seems to only be to display the details of a specific attachment with a given attachment_id value (ie: '?attachment_id=246').

Can someone steer me in the right direction?

2 Answers 2


attachment.php file displays only one attachment (like single.php or page.php).

There is no way to list all attachments without coding. WordPress doesn't have such list ready to use.

But it's rather easy to do it by yourself.

Add your custom page template. Assign it to some page. And place something like this inside this template:

$args = array(
       'post_type'=>'attachment',  // you want to show attachments
       'posts_per_page'=>-1,  // all of them
// other params

$attachments = new WP_Query( $args );
while ( $attachments->have_posts() ): $attachments->the_post();
//display attachment - current attachment is placed in global variable $post 
// you can also use template tags
  • Does not seem to work on admin screen.
    – Jules
    Commented Sep 15, 2020 at 8:52

The other solution here does not seem to work on admin screen. This worked for me:

$args = array('post_type'=>'attachment','numberposts'=>null,'post_status'=>null);
$attachments = get_posts($args);
    foreach($attachments as $attachment){
        // here your code 

Source: http://www.kvcodes.com/2015/12/get-attachments-wordpress/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.