Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Attachment Loop

Retrieves post type attachment

// WP_Query arguments
$args = array (
	'post_type'              => 'attachment',
	'cat'                    => '1',
	'category_name'          => 'sample',
	'year'                   => '2100',
	'monthnum'               => '4',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
	while ( $query->have_posts() ) {
		$query->the_post();
		// do something
	}
} else {
	// no posts found
}

// Restore original Post Data
wp_reset_postdata();