query attachments of parent page if attachments of current page are smaller than …

I’m using WPQuery and get_children($query_images_args) to query attachments from the current page. If the current page has no attachments assigned I’m querying the images of the parent page.

This works the way I want but there is one little extra I’m aiming for. I already asked a similar question (with the same code sample) here in the forum.
https://wordpress.stackexchange.com/questions/62624/only-get-attachments-that-are-associated-with-a-gallery
However in this question I ask for a different approach to address the same problem.

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

    $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');
    }

I’m querying the attachment images of the current page and if there are no images assigned to the current page I query the images of the parent page. Moreover I’m doing an additional filter-thingy for only large images.

However I wonder if it is possible to query the images of the parent page if the current page has only “small” images.

So in essence, I’d like to get only “large” images of the current post if it has images and get only “large” images of the parent page if the current post has no images.

Any ideas on how to do that!

Related posts

Leave a Reply

1 comment

  1. I’m not quite sure how you’re defining whether or not the current page has only “small” images. But I suspect the answer is right in the wp_get_attachment_image_src function, which returns not just the image url but also the width and height.

    So let’s say you were only interested in images that are more than 300 pixels wide. First, add this to your functions.php file:

    function return_my_big_images( $id ) {
    
        $query_images_args = array(
            'post_type' => 'attachment',
            'post_mime_type' => 'image',
            'post_status' => 'inherit',
            'posts_per_page' => -1,
            'post_parent' => $id
        );
    
        $attachments = get_children( $query_images_args );
    
        $big_images = array();
        foreach( $attachments as $image ) {
            $image_src = wp_get_attachment_image_src( $image->ID, 'full' );
            if ( $image_src[1] > 300 ) // $image_src[1] is where pixel width is stored
                $big_images[] = $image_src[0]; // $image_src[0] is its url
        }
    
        return $big_images;
    
    }
    

    Then, instead of your code above, you can add this:

    global $post;
    
    // return all images bigger than 300px wide attached to current post
    $big_images = return_my_big_images( $post->ID );
    
    // if none, return images bigger than 300px wide attached to post parent
    if ( empty( $big_images ) )
        $big_images = return_my_big_images( $post->post_parent );