Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I recently asked a question on how to get all images from all posts - and I got a working answer, but unfortunately the code gets post-thumnbnails as well, and I didn't find a way to limit the number of queried images.

My last question is here.

Thanks.

<?php
$query = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => -1 ) );
if( $query->have_posts() ){
while($query->have_posts()){
$query->the_post();
$thumb_ID = get_post_thumbnail_id( $post->ID );
$image_query = new WP_Query( array( 'post_type' => 'attachment', 'exclude' => $thumbID, 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_parent' => get_the_ID() ) );
while( $image_query->have_posts() ) {
$image_query->the_post();
echo '<dl class="gallery-item"><dt class="gallery-icon"><a rel="shadowbox[sbalbum-1];player=img;" href="' , the_permalink() , '">', wp_get_attachment_image( get_the_ID() ) , '</a></dt></dl>';
}
}
}
?>

Changing the number of the posts_per_page arg in the image_query has no effect, on the number of attachments displayed.

I came up with the following, working code with the help of @Bainternet:

<?php
$image_count = 0;
$max_images = of_get_option('thumbs_number');
$Posts = new WP_Query();
$Posts->query('post_type=post&post_status=publish&posts_per_page=-1');
    while ($Posts->have_posts()) : $Posts->the_post();    
    $args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $post->ID,
    'exclude' => get_post_thumbnail_id($id)
    );

$attachments = get_posts( $args );
    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
            if ($image_count < $max_images){
echo '<dl class="gallery-item"><dt class="gallery-icon"><a rel="shadowbox[sbalbum-1];player=img;" href="' , the_permalink() , '">';
echo wp_get_attachment_link( $attachment->ID, 'footer_thumbs' );
echo '</a></dt></dl>';
$image_count = $image_count + 1;
        }
    }
}
endwhile;?>
share|improve this question
    
Post alink to a pastebin containing the array of results from your query. Don't have a test setup for this right now. –  kaiser Dec 7 '11 at 16:41
    
Updated my Q with the code I'm trying to tweak, as you can see, it echos every every single post attachments including post-thumbnails. –  elbatron Dec 7 '11 at 16:52
    
No, I can't see. I only see you adding markup. PLEASE make a print_r of the resulting array, put it into a pastebin and link here. I'm not going to guess around. –  kaiser Dec 7 '11 at 16:57
    
Like this: pastebin.com/VYBxytvL ? –  elbatron Dec 7 '11 at 17:14
    
Close :) Could you wrap it into something like echo '<pre>'; print_r( $image_query ); echo '</pre>'; so it's more readable? Thanks! –  kaiser Dec 7 '11 at 17:23
show 1 more comment

1 Answer

up vote 0 down vote accepted

Try this:

$image_count = 0;
$max_images = 10;  //set this to the max number of images you want.
$ids = get_posts( array( 'post_type' => 'post', 'posts_per_page' => -1 ,'fields' =>'ids') );
if (count($ids) > 0){
    foreach ($ids as $id){
        while ($image_count < $max_images){
            $args = array(
                'post_type'   => 'attachment',
                'numberposts' => -1,
                'post_status' => null,
                'post_parent' => $id,
                'exclude'     => get_post_thumbnail_id($id)
            );
            $attachments = get_posts( $args );
            if ( $attachments ) {
                foreach ( $attachments as $attachment ) {
                    if ($image_count < $max_images){
                        echo '<dl class="gallery-item">
                            <dt class="gallery-icon">
                                <a rel="shadowbox[sbalbum-1];player=img;" href="' . get_permalink($attachment->ID) . '">'. wp_get_attachment_image( $attachment->ID ) . '</a>
                            </dt>
                         </dl>';
                         $image_count = $image_count + 1;
                    }
                }
            }
        }
    }
}

which is about the same as the example in the codex (Show All attachments for the current post beside Thumb ) combined with your code (almost) to get All attachments for all posts.

Mind the first query is changed to a get_posts() function with the parameter fields set to ids to get only the ids of the posts since you don't really need the rest of the data, and is much much faster.

share|improve this answer
    
With the following code I'm getting: Fatal error: Maximum execution time of 30 seconds exceeded in... –  elbatron Dec 7 '11 at 17:23
    
Isn't the second while loop going to push the counter to the end before the outer while loop has a chance to make a second iteration though? –  t31os Dec 7 '11 at 18:02
    
Yes but that is why its there. –  Bainternet Dec 7 '11 at 21:52
    
It'not working. Maximum execution time of 30 seconds exceeded in../wp-includes/query.php on line 1924 –  elbatron Dec 8 '11 at 9:06
    
Ok. I got the code working and it seems that t31os saw the problem with the it: I'm getting the same image 10x. –  elbatron Dec 11 '11 at 11:40
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.