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
- How to use a custom post type as front page?
- wp_generate_attachment_metadata gives me an empty array
- SQL to update custom post taxonomies
- Query multiple taxonomy in Custom Post Type
- 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
- 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
- Include both default and Custom Post Type in query modified inside pre_get_posts
- Stopping WordPress from Auto Generating Image Files for Sizes
- $wpdb returns no results with SELECT query on custom post type, works on default post type
- Include different loop templates in search query
- Custom Post Types, Page Templates and Pagination. Why do I get a 404 Error?
- Automatically adding meta data to posts or multiple query help
- Having trouble with custom date field for CPT query (WordPress)
- Custom Post Type order by modified date and custom meta field issue
- Impossible to get Attachments Outside WordPress?
- Issue to get wp_get_attachment_image with cmb2
- 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
- Querying Term Posts in Loop
- Use get_post_types to query only custom posts types
- Custom Post type is being ignored in query
- Custom Post Type – List all attachments on Edit Screen
- Disable featured image
- Am unable to reset a query properly
- get_attached_media() on author page not working
- Get next and previous 3 posts in a term in single post page
- Search / Filter posts on Title/Content OR Tags
- Meta query for custom post type ignored in main query
- Querying Two Custom Post Types with OR Not Working
- How do I move/order posts with a tag to the end?
- How can I get the number of custom post type posts that have a specific attachment image set?
- Hierarchy and access control for Custom Post Types (CPT)
- Searching Custom Post Types
- I am trying to hide a custom post type category to logged in users with Pre_Get_Posts
- How do you get the full size url of an attachment image
- Query multiple taxonomies with pre get posts
- Query Custom Post Types with checkboxes
- show all custom posts types in home page
- List of child custom post types lists all custom post types
- Make (custom) post type accessible only by custom query
- Returning a custom content types with meta values
- Custom Query: If One Post Object Field Value Is The Same As Another
- Custom Post Type not showing in main loop
- is_main_query() never called on WP 4.4
- Hide text if query has no posts
- How do I get the index for a custom post?
- Create query for both custom post type and category
- Filtering custom posts by custom fields (ACF) [closed]
- Custom taxonomy query showing more than 4 posts
- Let users upload image(s) to the post from front end
- Query Custom Post by taxonomy multiple categories
- Custom Post type showing up in loop, regular posts are not showing up
- How do I add multiple post types to this query?
- Why doesnt my tag page populate with this custom post type?
- How to divide Subcategories into pages of parent category wordpress
- WP Query: If field X is empty, show posts based on field Y
- Shortcode with loop stacks output
- Displaying all posts by category and showing content on click
- Custom attachment function not working in v5.4.2?
- Custom post type order by post_title
- WP_Query for custom taxonomies showing posts from non-specified terms?
- WP_Query with all posts in one custom post type and only posts in another custom post type with a specific category
- Dupplication of Custom Post Type while adding attachment
- How can I group by Taxonomy on Custom Post Type while filtering out based on custom Meta
- Get posts of an specific term of a custom taxonomy
- Conditional posts in WP_query for search
- Delete custom post type metadata without deleting the post in admin area
- Display year value before a set of posts
- Help ordering custom query by Title, Ascending
- How to add parameters for custom post type
- 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
- Delete attachment with post
- Query Only Show Text on Posts With Certain Taxonomy Tag
- Multiple Queries and Loops within CPT Archive
- Query Posts From Multiple Post Types
- Pagination not working with custom loop
- how to limit query_post to first page?
- 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
- 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
- Posts 2 Posts: query connected — orderby problem
- custom post-type query just returns two posts
- Order custom post type is beign ignored
- 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