WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I just wonder why WP_Query adding one more additional attachment with the defined number or posts. Also need to link attachment (thumbnail) to the post url. Here is my code

$query = new WP_Query( array( 'post_type' => 'news', 'showposts' => 5, 'orderby' => 'rand' ) );
if( $query->have_posts() ){
        $image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'showposts' => -1, 'post_parent' => get_the_ID() ) );
        while( $image_query->have_posts() ) {

            echo '<div class="attachment">';
            echo wp_get_attachment_image( get_the_ID() );
            echo '</div>';
share|improve this question

Your Answer


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

Browse other questions tagged or ask your own question.