I'm using the following script for my current project.

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

$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
    $images[]= wp_get_attachment_url( $image->ID );

This saves all image paths of the current post or page into an array. And right now the paths are set to the "original" uploaded file.

Is it possible to have a parameter that specifies that I want to save the largest file-size wordpress generates (with its settings in media) to that array?

So when uploading an image to WP the following happens …


Those four images are inside my uploads folder when I leave the default settings of the media uploader. And with my script above "image.jpg" gets pushed into the array. However I want the largest generated size in my array - in this case image-1025x843.jpg.

Is that possible?


correct me if I'm wrong but isn't image.jpg , your original always the largest?

-- if not you can split the urls on '-'. and get last from your sort? do take in mind you take last split, because it's possible your filename has a '-'. http://be.php.net/preg_split

  • Yeah you're right! In this case I just described my question wrong. I don't want to use the "largest" but the largest image wordpress generates based on my settings inside "media". So I don't want to use the "original" image i'm uploading (which is of course the largest as you say) but I want to use the second-largest. I'm able to set "Maxium Image Size" into my Media Backend of Wordpress. The default setting is "1024x768" and that's the image size I want to query. If I set the "Maximum Image Size" to "1280x1024" I want that size to be queried. – mathiregister Aug 8 '12 at 7:19
  • See this for example … codex.wordpress.org/Function_Reference/… … Here I would be able to set "large" as image size. That is what I'm looking for. – mathiregister Aug 8 '12 at 7:20
  • why not use my suggestion? check the urls with split/regex. – woony Aug 8 '12 at 7:34
  • But I do not even know how to query all those sizes. This is just what is in my "/uploads" folder not in my array. In my array there is always the original file-size. – mathiregister Aug 8 '12 at 7:38
  • oh ok, well you can just use codex.wordpress.org/Function_Reference/wp_get_attachment_image with you attachment ids? – woony Aug 8 '12 at 7:44

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.