Title: Using WP_Query to get attachments
Last modified: August 19, 2016

---

# Using WP_Query to get attachments

 *  [wdm](https://wordpress.org/support/users/wdm/)
 * (@wdm)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/using-wp_query-to-get-attachments/)
 * Is there a way to get attachments with WP_Query?
 * It seems to work only with get_posts():
 *     ```
       $attachments = new WP_Query('post_type=attachment'); // not working
   
       $attachments = query_posts('post_type=attachment'); // not working
   
       $attachments = get_posts('post_type=attachment'); // works
   
       var_dump($attachments);
       ```
   

Viewing 1 replies (of 1 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/using-wp_query-to-get-attachments/#post-1000170)
 * What about using [get_children](http://codex.wordpress.org/Function_Reference/get_children)?
 * For example, display thumbnail image for post in loop:
 *     ```
       <?php
       $images = get_children(
       array(
       'post_parent' => $post->ID,
       'post_status' => 'inherit',
       'post_type' => 'attachment',
       'post_mime_type' => 'image',
       'order' => 'ASC',
       'orderby' => 'menu_order'
       )
       );
       if ( $images ) {
       foreach ( $images as $id => $image ) {
       $img = wp_get_attachment_thumb_url( $image->ID );
       $link = get_permalink( $post->ID );
       print "\n\n" . '<img class="thumb" src="' . $img . '" alt="" />';
       }
       }
       ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Using WP_Query to get attachments’ is closed to new replies.

## Tags

 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [17 years, 2 months ago](https://wordpress.org/support/topic/using-wp_query-to-get-attachments/#post-1000170)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
