wp_query get attachments in larger size

If you look at the raw post data…

while (have_posts() ) { 
  the_post(); ?>
  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>><?php
      var_dump($post); ?>
  </article><?php 
}

You will see that there is nothing in there to specify “medium”, so I went looking. There is a filter on the_content

add_filter( 'the_content', 'prepend_attachment' );

… that juggles data for “attachment” post types.

1576    function prepend_attachment($content) {
1577            $post = get_post();
1578    
1579            if ( empty($post->post_type) || $post->post_type != 'attachment' )
1580                    return $content;
1581    
1582            if ( wp_attachment_is( 'video', $post ) ) {
1583                    $meta = wp_get_attachment_metadata( get_the_ID() );
1584                    $atts = array( 'src' => wp_get_attachment_url() );
1585                    if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
1586                            $atts['width'] = (int) $meta['width'];
1587                            $atts['height'] = (int) $meta['height'];
1588                    }
1589                    if ( has_post_thumbnail() ) {
1590                            $atts['poster'] = wp_get_attachment_url( get_post_thumbnail_id() );
1591                    }
1592                    $p = wp_video_shortcode( $atts );
1593            } elseif ( wp_attachment_is( 'audio', $post ) ) {
1594                    $p = wp_audio_shortcode( array( 'src' => wp_get_attachment_url() ) );
1595            } else {
1596                    $p = '<p class="attachment">';
1597                    // show the medium sized image representation of the attachment if available, and link to the raw file
1598                    $p .= wp_get_attachment_link(0, 'medium', false);
1599                    $p .= '</p>';
1600            }
1601    
1602            /**
1603             * Filter the attachment markup to be prepended to the post content.
1604             *
1605             * @since 2.0.0
1606             *
1607             * @see prepend_attachment()
1608             *
1609             * @param string $p The attachment HTML output.
1610             */
1611            $p = apply_filters( 'prepend_attachment', $p );
1612    
1613            return "$p\n$content";
1614    }

The relevant lines are 1597 and 1598. wp_get_attachment_link(), which in turn uses wp_get_attachment_image() (line 1545), which uses image_downsize()… and there is a promising filter:

167         if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) {
168                 return $out;
169         }

Et viola!

function image_size_hack() {
  $img_url = wp_get_attachment_url();
  $meta = wp_get_attachment_metadata();
  $width= $height="";
  if (!empty($meta['width'])) {
    $width = $meta['width'];
  }
  if (!empty($meta['height'])) {
    $width = $meta['height'];
  }
  return array( $img_url, $width, $height, false );
}
add_filter( 'image_downsize',  'image_size_hack');