2

I use this to query all attachments of the current page or post I'm on …

$query_images_args = array(
    'post_type' => 'attachment',
    'post_mime_type' =>'image',
    'post_status' => 'inherit',
    'posts_per_page' => -1,
    'post_parent' => $post->ID //$post->post_parent
);

$attachments = get_children($query_images_args);

if ( empty($attachments) ) {
    $query_images_args = array(
        'post_type' => 'attachment',
        'post_mime_type' =>'image',
        'post_status' => 'inherit',
        'posts_per_page' => -1,
        'post_parent' => $post->post_parent
    );
}

$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
    $images[] = wp_get_attachment_image_src( $image->ID, 'large');
}

foreach ( $images as $image) {
    if ( $image[1] >= 1024 )
        $large_images[] = $image[0];
}

if ( !empty($large_images) ):

This works perfectly and queries only the images of the current post or page.

However I have one additional thing I want this piece of code to do:

If a page has no images attached and is a "child-page" of a page that has images attached I want to query the images of the "parent-page".

Again:

parent-page -> has images attached child-page -> has no images attached -> should query images of the parent-page

This could be done with $post->post_parent

$query_images_args = array(
            'post_type' => 'attachment',
            'post_mime_type' =>'image',
            'post_status' => 'inherit',
            'posts_per_page' => -1,
            'post_parent' => $post->post_parent
        );

However I have no idea how to differentiate between those two cases.

If a page has images attached I want my code to be 'post_parent' => $post->ID, if a page is a child-page and has NO images attached I want the code to be 'post_parent' => $post->post_parent

Any ideas on that?

1 Answer 1

1

You can use get_children to check if it returns anything, then if it doesn't, fetch the parent attachments.

//loop stuff

//your $query_images_args

$attachments = get_children($query_images_args);

if ( empty($attachments) ) {

//go get parent attachments 

}else{

//loop through this post's attachments

}
3
  • Thank you, this works perfectly. However I have one last little problem: I did not post my entire code above because it was not necessary for the understanding the question. But I do filter all attachments by its size ( larger than 1024 ) -> I updated my question above with all the code I'm using now. This means … I get all image attachments and do another loop where I only get the images larger than 1024px in width and save them into $large_images … this is exactly what I need. Commented Aug 14, 2012 at 6:52
  • However is it also possible to query the size of the attachments with the get_children() function so I could also get the parent images if the child-page has only smaller images than 1024px? Sounds complicated and I can hardly explain it. Again: Generally everything is working fine now - thanks to your help. However there is one case that doesn't work. If a child-page has images (but those images are small (say like thumbnails)) I also want to get the large images of the parent-page. Any ideas on that too? Commented Aug 14, 2012 at 6:56
  • Here , on WPSE it best not to edit an already answered question as it goes against the flow of how the site works. It's best to leave your original question and ask a new one, this benefits other users a great deal, thanks!
    – Wyck
    Commented Aug 14, 2012 at 16:32

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.