2

I'm a fairly new WP developer. I am trying to get my portfolio.php to display all images attached to my custom post type 'portfolio', and then display them in a masonry format. I've managed to get them to show without the $post->post_type query, but not much success when trying to limit WP to retrieving images attached to my custom post type. Any help will be much appreciated, and thanks in advance!

    <?php get_header(); ?>
    <div id="portfolio-wrapper">
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if ( $post->post_type == 'portfolio'  ) {
    $attachments = get_posts( array (
        'post_type' => 'attachment',
        'numberposts' => -1,
        'post_parent' => $post->ID
    ) );

     if ( $attachments ) {
foreach ( $attachments as $attachment ) {
    $imgurl = wp_get_attachment_url ($attachment->ID);
            echo '<div class="portfolio-item">';
            echo '<a href="'.$imgurl.'" rel="lightbox[portfolio-home]"><img class="portfolio" src="'.$imgurl.'"></a>';
            echo '</div>';
        }

    }
}
    ?>
    </div>
    <?php endwhile; endif; ?>
    <?php get_footer(); ?>

As requested, my solution. Not very elegant, but it works:

    $query = new WP_Query( array( 'post_type' => 'portfolio', 'posts_per_page' => -1 ) );
    if( $query->have_posts() ){
        while($query->have_posts()){
            $query->the_post();
            $image_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status'         => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => -1, 'post_parent' => get_the_ID(), 'order' => 'DESC' ) );
    while( $image_query->have_posts() ) {
        $image_query->the_post();
        $imgurl = wp_get_attachment_url( get_the_ID() );
        echo '<div class="portfolio-item">';
        echo '<a href="'.$imgurl.'" rel="lightbox[portfolio-home]"><img class="portfolio" src="'.$imgurl.'"></a>';
        echo '</div>';
    }
}

}

3
  • Found an appropriate script. Had to use WP_Query. Thanks!
    – natnai
    Commented Apr 6, 2014 at 10:35
  • 2
    Please consider adding your solution as an answer, so that others can learn from it and question doesn't haunt site as unanswered. :)
    – Rarst
    Commented Apr 6, 2014 at 12:29
  • 1
    Add your "As requested, my solution. Not very elegant, but it works:" part as an answer in the answer block. Commented Apr 6, 2014 at 15:16

1 Answer 1

2

Your solution (edited incorrectly into the question) should be workable but you should be able to accomplish the same with fewer queries.

  1. Pull your portfolio IDs-- note the fields argument.
  2. Then pull your attachments with those IDs as the parent post.
  3. Then you only loop over the one image array.

That works out to two primary queries. To wit:

$query = new WP_Query( 
  array( 
    'post_type' => 'portfolio', 
    'posts_per_page' => -1,
    'fields' => 'ids'
  ) 
);
$image_query = new WP_Query( 
  array( 
    'post_type' => 'attachment', 
    'post_status' => 'inherit', 
    'post_mime_type' => 'image', 
    'posts_per_page' => -1, 
    'post_parent__in' => $query->posts, 
    'order' => 'DESC' 
  ) 
);

if( $image_query->have_posts() ){
  while( $image_query->have_posts() ) {
      $image_query->the_post();
      $imgurl = wp_get_attachment_url( get_the_ID() );
      echo '<div class="portfolio-item">';
      echo '<a href="'.$imgurl.'" rel="lightbox[portfolio-home]"><img class="portfolio" src="'.$imgurl.'"></a>';
      echo '</div>';
  }
}

The way you are doing it would mean one query for the portfolio posts, plus another image query for each portfolio result. That could mean dozens, hundreds, or even more queries depending on the size of your database.

1
  • Thank you! Your solution is far more elegant than mine. I will be using this instead.
    – natnai
    Commented Apr 7, 2014 at 1:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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