It seems like a bit of a waste to run through two loops just to use some built in API functions that weren’t designed for a use case like this.
I think you’re better off to use your SQL combined with the wpdb class — faster and cleaner.
Example (with modified SQL):
<?php
function wpse52315_get_attach()
{
global $wpdb;
$res = $wpdb->get_results("select p1.*
FROM {$wpdb->posts} p1, {$wpdb->posts} p2
WHERE p1.post_parent = p2.ID
AND p1.post_mime_type LIKE 'image%'
AND p2.post_type="your_cpt"
ORDER BY p2.post_date
LIMIT 10;"
);
return $res;
}
Related Posts:
- Pagination not working with custom loop
- Creating an Image-Centric Custom Post Type?
- Submit post and upload image from front-end
- Get the ID of the latest post
- Correctly delete posts with meta and attachments [duplicate]
- Sort custom post type list table by display name of a user id stored as post meta value
- How to use a custom post type as front page?
- Attaching media to custom posts without editor
- Random post, once per day
- Query custom post type by custom field
- List most recent image uploads, but only for specific custom post type
- Add Custom Post Type to Current Query
- Custom Queries: Joining On Meta Values From Two Custom Post Types
- wp_generate_attachment_metadata gives me an empty array
- Add custom field to media attachment image attribute in post editor
- SQL to update custom post taxonomies
- Query multiple taxonomy in Custom Post Type
- Querying Posts by Taxonomy From Alternate Network Site
- Custom Search | Wrong output & question
- Get the most popular terms for a custom post type
- Query by 2 values of a repeater ACF field
- Count all images of a certain post type
- How do you modify the ‘post_parent’ of a custom post type?
- show posts only on homepage but using custom post type and taxonomy for query posts via url
- Comparing timestamps in meta query doesn’t work
- Display two post types ordered by two custom fields
- WP insert post PHP function dynamically generated Custom Fields
- Include both default and Custom Post Type in query modified inside pre_get_posts
- Stopping WordPress from Auto Generating Image Files for Sizes
- Query posts between two dates (custom meta keys) CMB2
- $wpdb returns no results with SELECT query on custom post type, works on default post type
- Include different loop templates in search query
- Do attachments added to custom post types have a post parent?
- Adding posts of custom type into the main feed
- Custom Post Types, Page Templates and Pagination. Why do I get a 404 Error?
- Inserting data into `post meta` table?
- Custom Select Query with Custom Post Types
- Automatically adding meta data to posts or multiple query help
- Having trouble with custom date field for CPT query (WordPress)
- Return Attachments from Custom Post Type
- How to get attached file in wordpress from custom post
- Custom Post Type order by modified date and custom meta field issue
- Impossible to get Attachments Outside WordPress?
- Sort posts based on multiple custom fields
- WP_Query search posts by custom post type and custom taxonomy
- Issue to get wp_get_attachment_image with cmb2
- Calculating Bayesian average for custom post type
- Conditional to modify query results
- Is it possible to create a shortcode that will query a post based on taxonomies?
- Selecting New & Edit Page for Custom Post Types
- Show Two custom Post type and their posts on category page
- How to filter custom taxonomy categories on archive?
- Modifying date filter on admin page for custom post type to link to custom field
- Ignore sticky posts if post is not in meta query
- Querying Term Posts in Loop
- How to debug a blank page?
- Query Pulling the same post twice
- Is it possible to add query parameters on the archive page?
- meta query always fails even if data is correct
- How do I get a nested query to only display content that the main query outputed
- $post breaking container loop
- Pagination in custom loop for custom post type throwing 404 error
- Delete attachment with post
- Query Only Show Text on Posts With Certain Taxonomy Tag
- 404 for children in hierarchical custom post type
- Multiple Queries and Loops within CPT Archive
- Query child posts on parents single.php?
- Query Posts From Multiple Post Types
- Pagination not working with custom loop
- how to limit query_post to first page?
- Filter CPT based on meta box value using Flexible Posts widget?
- WordPress can only query up to 766
- How would I get a list of posts from a custom post type
- Pagination not working with custom loop
- Pagination not working with custom loop
- Post content stays the same but permalink changes ?
- Pagination not working with custom loop
- Retrieve post data via WPDB class
- $wpdb returns no results with SELECT query on custom post type, works on default post type
- Listing custom post type items from a couple of custom taxonomies
- Pagination not working with custom loop
- Show custom posts with a specific “custom TAXONOMY slug” on startpage
- How to grab data (titles, thumbnails and custom fields) from multiple posts to populate a new array efficiently?
- Allow admin to determine the order of queries?
- WordPress search results
- Posts 2 Posts: query connected — orderby problem
- Code in custom widget queries all posts, when it should only query the current post
- custom post-type query just returns two posts
- Order custom post type is beign ignored
- How to add post type in masonry posts elementory widget
- Filter Custom post type by another Custom post type
- Create a list of months based posts
- WordPress Admin Page Slow. Customer Post Type is the slowest
- Add a class to a menu item depending on a body class
- Get Posts ordered by a date custom meta field
- Display all child posts in a custom post type, grouped by a custom taxonomy site
- Load posts via AJAX without draft status
- Display custom post type attached media file sizes
- How can I show a field ID in Elementor with Request Parameter
- Query Multiple Post Types and Paginate Newly Created List