Title: Filter and display attachments
Last modified: August 19, 2016

---

# Filter and display attachments

 *  Resolved [mohawktopus](https://wordpress.org/support/users/mohawktopus/)
 * (@mohawktopus)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/)
 * Hello all, this should be a quick fix (hah!), I just don’t have the php skills.
 * This is in my `sidebar.php` displaying **all** media uploaded to my wordpress
   installation. I want to filter it so that it only will display media if it has
   certain file extensions, (i.e. pdf, doc, rtf). What do I need to change/add in
   order for that to happen?
 *     ```
       <?php
   
       $args = array(
       	'post_type' => 'attachment',
       	'numberposts' => -1,
       	'post_status' => null,
       	'post_parent' => null, // any parent
       	);
       $attachments = get_posts($args);
       if ($attachments) {
       	foreach ($attachments as $post) { ?>
       <li>
       <?php
       		setup_postdata($post);
       		the_attachment_link($post->ID, true);
       ?>
       </li>
   
       <?php
       	}
       }
   
       ?>
       ```
   

Viewing 9 replies - 1 through 9 (of 9 total)

 *  Thread Starter [mohawktopus](https://wordpress.org/support/users/mohawktopus/)
 * (@mohawktopus)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955371)
 * I found this snippet:
 *     ```
       <?php
       // list of approved file extensions
       $approved = array (
           'pdf' => "application/pdf",
           'doc' => "application/msword",
           'gif' => "image/gif",
           'jpg' => "image/jpeg",
           'jpeg' => "image/jpeg"
       };
   
       // for display purposes
       $filename = "index.html";
   
       // get the filename
       $ext = pathinfo($filename);
   
       switch (array_key_exists($ext['extension'], $approved)) {
           case true:
               break; // do nothing, break out...
           case false:
               echo $ext['extension'] ." is invalid!";
               exit;
           break;
       }
   
       ?>
       ```
   
 * Is there anyway to integrate this with the above to solve my problem?
 *  Thread Starter [mohawktopus](https://wordpress.org/support/users/mohawktopus/)
 * (@mohawktopus)
 * [17 years, 4 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955611)
 * I finally made a solution! I feel so proud 🙂
 * Put this in `sidebar.php`:
 *     ```
       <li><h2>Recent Attachments</h2>
       <ul>
       <?php
       $valid_ext = array("pdf", "doc", "rtf", "zip");
       $args = array(
       	'post_type' => 'attachment',
       	'numberposts' => -1,
       	'post_status' => null,
       	'post_parent' => null, // any parent
       	);
       $attachments = get_posts($args);
       if ($attachments) {
       	foreach ($attachments as $post) {
       $ext = getFileExt(wp_get_attachment_url($post->ID, false));
       if(in_array($ext, $valid_ext)) {
        ?>
       <li>
       <?php
       	setup_postdata($post);
       	the_attachment_link($post->ID, true);
       ?>
       </li>
       <?php
                       }
       	}
       }
       ?>
       ```
   
 * And define `getFileExt()` in wp-includes/functions.php by pasting this at the
   bottom (before the final `?>`):
 *     ```
       function getFileExt($file) {
            return strtolower(substr(strrchr($file,'.'),1));
       }
       ```
   
 * This will put the latest uploads of particular extensions (defined by you in 
   the `$valid_ext` array) in your sidebar. I needed this because I’m creating a
   website for a Boy Scout troop, and the latest permission slips and agendas need
   to be on the front page, while any images or other media they may upload do not.
 * Hope this helps out someone else, too.
 *  [Mark Bloomfield](https://wordpress.org/support/users/yellowllama/)
 * (@yellowllama)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955763)
 * thank you! this is almost exactly what i’m looking for! 😛
 * i’m needing this list to generate the list of attachments of the **current post**
 * can the above script be tailored to accomplish this?
 * thanks for your help!
    regards
 *  [Edde](https://wordpress.org/support/users/edde/)
 * (@edde)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955810)
 * [@yellowllama](https://wordpress.org/support/users/yellowllama/): you might want
   to change
    `'post_parent' => null, // any parent` in `'post_parent' => $post-
   >ID,`
 *  [Edde](https://wordpress.org/support/users/edde/)
 * (@edde)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955811)
 * OK. That was a bit short. I’ve puzzled together the following, grabbing code 
   from forum. This creates a bullet list of **.pdf** and **.doc** attachments.
 *     ```
       <ul>
           <?php
               $args = array(
                 'post_type' => 'attachment',
                 'post_mime_type' => 'application/pdf,application/msword',
                 'numberposts' => -1,
                 'post_status' => null,
                 'post_parent' => $post->ID,
                 'orderby' => 'menu_order',
                 'order' => 'desc'
                 );
               $attachments = get_posts($args);
               if ($attachments) {
                 foreach ($attachments as $attachment) {
                   echo '<li><a href="'.wp_get_attachment_url($attachment->ID).'">';
                   echo $attachment->post_title;
                   echo '</a></li>';
                 }
               }
           ?>
           </ul>
       ```
   
 *  [tbuyle](https://wordpress.org/support/users/tbuyle/)
 * (@tbuyle)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955836)
 * Works just great for me. Thanks 1
 *  [anabelle](https://wordpress.org/support/users/anabelle/)
 * (@anabelle)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955837)
 * Thanks 🙂
 *  [patricia1706](https://wordpress.org/support/users/patricia1706/)
 * (@patricia1706)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955839)
 * I’ve got code similar to edde, only mine is with images.
    In this line:`echo '
   <li><a href="'.wp_get_attachment_url($attachment->ID).'">';` I’m having difficulty
   having the title of the image ($attachment->post_title) being dynamically inserted
   into the a href call: `echo '<li><a href="'.wp_get_attachment_url($attachment-
   >ID).'" rel="shadowbox">';` I’ve got the rel=”shadowbox” so that the text link
   will pop open an image shadowbox (this is working). In order to have a title 
   on the pop up window I need to include this within the a href string…can any 
   one offer a suggestion? Thanks!
 *  [patricia1706](https://wordpress.org/support/users/patricia1706/)
 * (@patricia1706)
 * [16 years, 4 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955840)
 * Figured out in case anyone will have the same problem in the future:
 *     ```
       <?php
               $args = array(
                 'post_type' => 'attachment',
                 'post_mime_type' => 'image',
                 'numberposts' => -1,
                 'post_status' => null,
                 'post_parent' => $post->ID,
                 'orderby' => 'menu_order',
                 'order' => 'ASC'
                 );
               $attachments = get_posts($args);
               if ($attachments) {
                 foreach ($attachments as $attachment) {
                   echo '<li><a href="'.wp_get_attachment_url($attachment->ID).'" rel="shadowbox" title="'.$attachment->post_excerpt.'">';
                   echo $attachment->post_title;
                   echo '</a></li>';
                 }
               }
           ?>
       ```
   
 * This will display the image caption in the shadow box.

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Filter and display attachments’ is closed to new replies.

## Tags

 * [attachment](https://wordpress.org/support/topic-tag/attachment/)
 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [media](https://wordpress.org/support/topic-tag/media/)

 * 9 replies
 * 6 participants
 * Last reply from: [patricia1706](https://wordpress.org/support/users/patricia1706/)
 * Last activity: [16 years, 4 months ago](https://wordpress.org/support/topic/filter-and-display-attachments/#post-955840)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
