List most recent image uploads, but only for specific custom post type

Did you try adding a filter to get_posts. This isn’t tested, just a thought after a bit of searching :

 function my_filter( $query )
 {
      if( is_home() ) //Example, if u want to display recent images on home
      {
           $query->set( 'post_type', array( 'attachment', 'my_cpt' ) );
      }
 }
 add_filter( 'pre_get_posts', 'my_filter' );

EDIT :
After rereading, I don’t think it accomplishes what you’re trying to do, I think it will display both attachments, and posts of your CPT.

Guess i’ill leave it here in case it gives you any ideas.

EDIT 2:
The only other way besides filtering in PHP that I can think of would be a custom sql query, and then setting up the post data. EX :

$sql = "SELECT * FROM wp_posts
        WHERE post_type="attachment"
        AND post_parent IN (SELECT ID FROM wp_posts WHERE post_type="your-cpt")
        //ORDER BY";

 $posts = $wpdb->get_results( $sql, OBJECT );

 //loop - setup_postdata

More info: WordPress Codex

Leave a Comment