Get list of posts from attachment

I was able to find some helpful articles, the combination of which helped me solve this. First, I discovered that the uploadedTo information can retrieved, returning the ID. A bunch of information on attachment data can be found here.

Second, I was able to convert the ID into a link for the post by simply adding some html <a href="https://wordpress.stackexchange.com/?p=".$attachment_data['uploadedTo'].'">.

Third, I was able to convert the ID into the name of the post, using the method found here.

All together, I was able to generate a formatted link to the post, from the attachment.

Unfortunately, it won’t be a list of posts, since WordPress only allows posts to have one image attached (even if there technically are more).

The final code:

$the_query = new WP_Query( array(
    'post_type' => 'attachment',
    'category_name' => 'photos',
    'posts_per_page' => 25,
));

while ( $the_query->have_posts() ) : $the_query->the_post(); 

$attachment_data = wp_prepare_attachment_for_js($attachment);
$title = get_post_field( 'post_title', $attachment_data['uploadedTo'] );

      echo '<div><div>
      <img src="'.wp_get_attachment_url ('medium').'"/>
      </div><div><a href="https://wordpress.stackexchange.com/?p=".$attachment_data['uploadedTo'].'">'.$title.'</a></div>';

      endwhile; wp_reset_postdata();?>