Title: WordPress attachments in query_posts&#8230;
Last modified: August 20, 2016

---

# WordPress attachments in query_posts…

 *  [richgcook](https://wordpress.org/support/users/richgcook/)
 * (@richgcook)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wordpress-attachments-in-query_posts/)
 * Morning,
 * I need some guidance on some WP code. I’m wondering if it’s possible to take 
   this code (which is grabbing all the images from the posts across my site and
   dumping them out):
 *     ```
       $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
           $attachments = get_posts( $args );
           if ($attachments) {
       	foreach ( $attachments as $post ) {
       		setup_postdata($post);
       		the_attachment_link($post->ID, true);
       		the_excerpt();
       	}
           }
       ```
   
 * and use the `query_posts();` function in WP instead so I could create some pagination
   for all the images.
 * Thanks in advance,
    R

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

 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wordpress-attachments-in-query_posts/#post-2895271)
 * Try it with a new WP_query. get_posts() makes use of the WP_Query class to fetch
   posts.
    [http://codex.wordpress.org/Function_Reference/WP_Query](http://codex.wordpress.org/Function_Reference/WP_Query)
 *  Thread Starter [richgcook](https://wordpress.org/support/users/richgcook/)
 * (@richgcook)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wordpress-attachments-in-query_posts/#post-2895277)
 * I seem to have got something working with this below, but it’s pulling in one
   post, infinitely (see here: [http://goo.gl/4IlWG](http://goo.gl/4IlWG))
 *     ```
       <?php $ID = get_the_ID();
       	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       	query_posts("order=DESC&orderby=post_date&posts_per_page=8&paged=$paged"); while ( have_posts() ) : the_post(); ?>
   
       		<?php
       		$args = array( 'post_type' => 'attachment', 'post_status' => null, 'post_parent' => null, );
       		$attachments = get_posts( $args );
       		if ($attachments) {
       			foreach ( $attachments as $post ) {
       				setup_postdata($post);
       				the_attachment_link($post->ID, true);
       				the_excerpt();
       			}
       		}
       		?>
   
       	<?php endwhile; ?>
       ```
   
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wordpress-attachments-in-query_posts/#post-2895279)
 * Try it with this:
 *     ```
       foreach ( $attachments as $attachment ) {
       				//setup_postdata($post);
       				the_attachment_link($attachment->ID, true);
       				//the_excerpt();
       			}
       ```
   
 *  Thread Starter [richgcook](https://wordpress.org/support/users/richgcook/)
 * (@richgcook)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wordpress-attachments-in-query_posts/#post-2895286)
 * Thanks for your help with this, keesiemeijer.
 * Still the same problem – you can see it on that site again.
 *  Moderator [keesiemeijer](https://wordpress.org/support/users/keesiemeijer/)
 * (@keesiemeijer)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wordpress-attachments-in-query_posts/#post-2895288)
 * The default view is working so it must be something in your javascript.
 *  Thread Starter [richgcook](https://wordpress.org/support/users/richgcook/)
 * (@richgcook)
 * [13 years, 11 months ago](https://wordpress.org/support/topic/wordpress-attachments-in-query_posts/#post-2895289)
 * It’s the grid view I’m looking at ([http://itsfoolsgold.com/grid/](http://itsfoolsgold.com/grid/))
   the default and grid views both use different templates.

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

The topic ‘WordPress attachments in query_posts…’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [richgcook](https://wordpress.org/support/users/richgcook/)
 * Last activity: [13 years, 11 months ago](https://wordpress.org/support/topic/wordpress-attachments-in-query_posts/#post-2895289)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
