title: Limit posts with attachment inside Wordpress custom post type query
body: <p>I´m trying to get the post thumbnail and another attached image from the last post of a custom post type (called <code>parceiros-e-links</code>).</p>
  
  <p>What I got is to get all posts with images and show them... but I need to show only the last post and show it with two images (<code>the_post_thumbnail</code> and the <code>wp_get_attachment_image</code>)</p>
  
  <p>Here is my current code:</p>
  
  <pre><code>&lt;?php           
  $query = new WP_Query( array( 'post_type' =&gt; 'parceiros-e-links', 'posts_per_page' =&gt; -1, 'orderby' =&gt; 'date', 'order' =&gt; 'DESC' ) ); //the first loop where I filter the posts from custom post type and by date
  if( $query-&gt;have_posts() ){
      while($query-&gt;have_posts()){
      $query-&gt;the_post();
  
          $image_query = new WP_Query( array( 'post_type' =&gt; 'attachment', 'post_status' =&gt; 'inherit', 'post_mime_type' =&gt; 'image', 'posts_per_page' =&gt; 2, 'post_parent' =&gt; get_the_ID() ) );  //the second loop where I filter the posts with attachments and limit to two
          while( $image_query-&gt;have_posts() ) { $image_query-&gt;the_post();
                      //code below prints the two attachments: thumbnail and another one.
                  if(has_post_thumbnail()){
                      the_post_thumbnail('home-parceiros');
                  }
                  echo wp_get_attachment_image( get_the_ID(), 'home-parceiros-foto' );
          }
      }
  }
  ?&gt;
  </code></pre>
  
  <p>I already spent many hours searching similar situations and trying to filter this code but I'm without ideas... Because if I limit the query's first <code>posts_per_page</code> to 1, if the most recent post doesn't has an attachment, no images will be printed!</p>
  
  <p>Any clues about how to limit the number of posts with attachments inside a custom post type?</p>
  
  <p>Thanks!</p>