1

I'm having issues with a query. I want to get the images attached to an individual post of the type rental.

My query is broken as it outputs all the images on the entire site:

$image_query = new WP_Query( 
    array( 
        'post_type' => 'attachment', 
        'post_status' => 'inherit', 
        'post_mime_type' => 'image', 
        'posts_per_page' => -1, 
        'post_parent' => $rental->post->ID,
        '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="item">';
     echo '<img src="'.$imgurl.'">';
     echo '</div>';
 }

 wp_reset_postdata();

}

Any ideas on how I can adjust this to only get the images attached to the current post?

  • 1
    Clue: what is $rental? – ngearing Dec 21 '16 at 0:54
  • $rental is my custom post type. I get the same result if I just use 'post_parent' => $post->ID there too. – warm__tape Dec 21 '16 at 2:33
  • Okay, does $rental->post->ID give us the correct data? – ngearing Dec 21 '16 at 4:13
  • No, that gives me the same set of data as $post->ID. Its a weird one thats for sure. – warm__tape Dec 21 '16 at 4:19
0

Your issue is the post_parent parameter, if this is set to 0 or a falsey value like null your query will return all top level posts.

So you just have to make sure $rental->post->ID has a value, or use a different approach like the get_the_ID() function.

Can't really tell you more than that as you have not included the part where you set $rental

$image_query = new WP_Query( 
    array(
        'post_type' => 'attachment',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
        'post_parent' => get_the_ID(), // your issue here lays
    )
);

And check out the docs for more details: https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

0

Yeah, that helped. I noticed that the function worked perfectly fine on a standard post. I basically ended up moving the function into functions.php:

function get_rental_carousel_images($rental_id) {

// loosely based on http://martyspellerberg.com/2011/11/outputting-the-wordpress-attachments-gallery/

global $rental;

$images = get_children( array(
    'post_parent' => $rental_id,
    'post_status' => 'inherit',
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    'orderby' => 'menu_order ID'
) );

if ($images) :

    foreach ($images as $attachment_id => $image) :

        $img_url = wp_get_attachment_url( $image->ID );

        ?>

        <img src="<?php echo $img_url; ?>" />

    <?php endforeach; ?>

<?php endif;

and then outputting images in my template like this:

<?php get_rental_carousel_images( get_the_ID() ); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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