Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Get All Post Attachments in WordPress Except for Featured Image

Last updated on by
Follow WPBeginner on YouTube
How to Get All Post Attachments in WordPress Except for Featured Image

Recently while working on a custom project for a client, we had to get all post attachments from a custom post type and display them at one place. Because we were creating a grid display, we had each post’s featured image serving the purpose of a separator. This is why when getting all post attachments, we needed to exclude the featured image, so it doesn’t show up twice. In this article, we will show you how to get all post attachments in WordPress except for the featured image.

All you have to do is paste the following code inside a loop.

<?php if ( $post->post_type == 'data-design' && $post->post_status == 'publish' ) {
		$attachments = get_posts( array(
			'post_type' => 'attachment',
			'posts_per_page' => -1,
			'post_parent' => $post->ID,
			'exclude'     => get_post_thumbnail_id()
		) );

		if ( $attachments ) {
			foreach ( $attachments as $attachment ) {
				$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
				$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
				echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
			}
			
		}
	}
?>

The code above first checks if the post type is data-design and the post status is published. You may not need the first conditional depending on what you are trying to do. Then we simply run the get_posts query. Everything is pretty self explanatory there. The key that we must highlight is the exclude feature. That line is making sure that featured image does not show up. If you take that line away, then the featured image will show up. After specifying all the parameters, we simply specify what to do when each attachment is pulled. We are pulling attachment type for the class variable. Then the $thumbimg variable is simply using wp_get_attachment_link to pull the image at a specific thumbnail size, and it also hyperlinks the image to the single attachment pages. In the final step, we simply echo it.

We ran this code inside a loop with a separate call for featured image which links to the individual post. Final outcome looked something like this:

Attachment Grid

Each featured image served as an album identifying image which you can see. The grey spots were filled with the attachments for the post. We hope that this would help those who are looking to push WordPress beyond a blogging platform.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Angel

    I’ve inserted this code, but when I use it the page is blank. I’ve changed the custom post type name to match my own custom post type but still nothing shows.

    Here’s the code:

    post_type == ‘gallery’ && $post->post_status == ‘publish’ ) {
    $attachments = get_posts( array(
    ‘post_type’ => ‘attachment’,
    ‘posts_per_page’ => -1,
    ‘post_parent’ => $post->ID,
    ‘exclude’ => get_post_thumbnail_id()
    ) );

    if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
    $class = “post-attachment mime-” . sanitize_title( $attachment->post_mime_type );
    $thumbimg = wp_get_attachment_link( $attachment->ID, ‘thumbnail-size’, true );
    echo ” . $thumbimg . ”;
    }

    }
    }
    ?>

    Any help would be appreciated.

    • Angel

      sorry I accidentally clipped the code:

      post_type == ‘gallery’ && $post->post_status == ‘publish’ ) {
      $attachments = get_posts( array(
      ‘post_type’ => ‘attachment’,
      ‘posts_per_page’ => -1,
      ‘post_parent’ => $post->ID,
      ‘exclude’ => get_post_thumbnail_id()
      ) );

      if ( $attachments ) {
      foreach ( $attachments as $attachment ) {
      $class = “post-attachment mime-” . sanitize_title( $attachment->post_mime_type );
      $thumbimg = wp_get_attachment_link( $attachment->ID, ‘thumbnail-size’, true );
      echo ” . $thumbimg . ”;
      }

      }
      }
      ?>

  • Toure

    I tried the code about and could not get the attachment for a cpt
    I defined the cpt type by this query first:

        <?php $query = new WP_Query( array( 'post_type' => 'news', 'paged' => $paged ));
    		if ( $query->have_posts() ) : ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    <?php
    if ( $post->post_type == 'people' && $post->post_status == 'publish' ) {
    		$attachments = get_posts( array(
    			'post_type' => 'attachment',
    			'posts_per_page' => -1,
    			'post_parent' => $post->ID,
    			'exclude'     => get_post_thumbnail_id()
    		) );
    
    		if ( $attachments ) {
    			foreach ( $attachments as $attachment ) {
    				$class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
    				$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
    				echo '<li class="' . $class . ' data-design-thumbnail">' . $thumbimg . '</li>';
    			}
    			
    		}
    	}
    ?>
          <?php endwhile;?>
          <?php endif; ?>
    
    
  • Sankalp

    how can i display full image in attachment not its thumbnail

    • http://www.wpbeginner.com Editorial Staff

      Look in line 12 of the code where it says thumbnail-size. That’s really the image size. You can use one of the pre-existing sizes to show the image. The value “full” there would do the job.

  • phillip

    problem:
    1. insert a image into a post and press save
    2. reload page with your code inside the loop, it works great
    3. now remove the image from the post and save
    4. reload and the image still there!!!!
    This ins’t correct and I can’t find a solution for this on the internet

    Thanks

    • http://www.wpbeginner.com Editorial Staff

      Are you removing the image from the post? or are you deleting the image altogether? Because if you just remove the image code from the post content, then it will not go away. The image stays attached to the post it was uploaded too unless you DELETE it.

      • Phillip

        That’s what I figured it out.
        Doesn’t make sense to me.

        If you want to remove an image from a post and keep it for later use, the attachment will continue to appear using your code. I have to delete it!?

        If instead of removing you just want to reuse the image in a second post, that second post will not show the image as attachment because there’s only one field for parent reference – parent_id – can’t point to 2 different IDs. I have to upload a second file equal to first one!?

        This are very simples tasks that don’t work out of the box. Why is wordpress working like this? I believe that must be a very strong reason for that…

  • Pete

    Doesn’t seem to work for me?

    • Pete

      Got it working, I deleted this part…
      ‘$post->post_type == ‘data-design’ && ‘

      • http://www.wpbeginner.com Editorial Staff

        Glad you were able to get it to work.

        • Pete

          I think you should rename the post … “How to Get All Post Attachments in WordPress Except for Featured Image from a custom post type” as others like myself might see it as pertaining to all/any posts (which it doesn’t)… just might help (especially with search results) :)

        • http://www.wpbeginner.com Editorial Staff

          You can easily do what you did above, and it works.

  • shankarsoma

    Dear Webmaster

    You have not mentioned where to add codes in WordPress files is it functions.php, post.php or other things

    Do please elaborate the same

    • http://www.wpbeginner.com/ wpbeginner

      @shankarsoma You have to paste it inside the loop. Most of the time, you will create a custom page and post it in the loop. In our case, we created a custom post type and ran it in that loop. So it is very subjective which file it will go in … It all depends on how you use it.