Title: WordPress Query For Attachments
Last modified: August 20, 2016

---

# WordPress Query For Attachments

 *  Resolved [Ali Hussain](https://wordpress.org/support/users/b4db0y/)
 * (@b4db0y)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/wordpress-query-for-attachments/)
 * I have the following query
 *     ```
       <?php // gets attached images
       			$counter = 3;
       			$args = array(
       				'post_type' => 'attachment',
       				'numberposts' => -1, // posts_per_page doesn't work for get_posts!
       				'orderby' => 'rand',
       				'caller_get_posts' => 1,
       				'post_mime_type' => 'image',
       				'post_status' => null,
       				'post_parent' => null, // any parent
       				);
       			$attachments = get_posts($args);
       			if ($attachments) {
       				echo '<ul>';
       				// count number of available images and change if less than the specified limit
       				if ( count($attachments) < $counter )
       					$counter = count($attachments);
       				foreach ($attachments as $post) {
       					if ( $counter > 0 ) {
       						setup_postdata($post);
       						$image = wp_get_attachment_image_src( $post->ID, 'thumbnail', false );
       						echo '<li class="media"><a href="'.get_permalink().'" title="'.get_the_title().'" style="background-image: url('.$image[0].');">'.get_the_title().'</a></li>';
       						$counter--;
       					}
       				}
       				echo '</ul>';
       			}
       			?>
       ```
   
 * What I am unable to figure out on how to add a custom_post_type that i have created
   to it.
 * Thanks

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

 *  [Chris Talkington](https://wordpress.org/support/users/ctalkington/)
 * (@ctalkington)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/wordpress-query-for-attachments/#post-2477153)
 * I am guessing you want to get attachments from all posts from your custom post
   type? Although you mention attached images so maybe you are talking about all
   attachments on a single custom type post. If you could be a bit more clear, I
   might have some example code for your needs.
 *  Thread Starter [Ali Hussain](https://wordpress.org/support/users/b4db0y/)
 * (@b4db0y)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/wordpress-query-for-attachments/#post-2477154)
 * I got this working on my own.
    Thanks though
 *     ```
       <?php
       remove_all_filters('posts_orderby');
       query_posts('showposts=3&post_type=image&orderby=rand');
       global $more; $more=0;?>
   
       <?php if (have_posts) : while (have_posts()) : the_post(); global $more; $more=0;?>
   
       <?php
       $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'orderby' => 'rand', 'post_parent' => $post->ID );
       			$attachments = get_posts($args);
       			if ($attachments) {
       				echo '';
       				// count number of available images and change if less than the specified limit
       				foreach ($attachments as $post) {
       						setup_postdata($post);
       						$image = wp_get_attachment_image_src( $post->ID, 'thumbnail', false );
       						echo '<span class="media"><a href="'.get_permalink().'" title="'.get_the_title().'" style="background-image: url('.$image[0].');">'.get_the_title().'</a></span>';;
       				}
       				echo '';
       			}
       			?>
   
       <?php endwhile; endif; ?>
       ```
   

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

The topic ‘WordPress Query For Attachments’ is closed to new replies.

## Tags

 * [attachment](https://wordpress.org/support/topic-tag/attachment/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Ali Hussain](https://wordpress.org/support/users/b4db0y/)
 * Last activity: [14 years, 5 months ago](https://wordpress.org/support/topic/wordpress-query-for-attachments/#post-2477154)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
