I’d try to get rid of custom query altogether. Try something like:
$attachments = get_posts(array(
'post_type' => 'attachment',
'post_parent' => get_the_ID(),
'numberposts' => 1,
));
Related Posts:
- Creating an Image-Centric Custom Post Type?
- Correctly delete posts with meta and attachments [duplicate]
- modify all posts of a category to “no comments allowed”
- Add custom field to media attachment image attribute in post editor
- Impossible to get Attachments Outside WordPress?
- Deleting Custom Post type data using mySQL command
- Custom Post Type – List all attachments on Edit Screen
- Group posts by custom post type
- Custom post type tags not showing in search
- $wpdb is queried but results don’t show
- One post auto attached to many another items
- Delete attachment with post
- ACF – Tags not showing up in Custom Post Types
- Weird problem happening with custom taxonmy when creating/updating posts
- Placeholder in HTML editor text area?
- How can I create a custom meta box to add an mp4 video to a page?
- wpdb custom post_type problem
- Adding custom tables to WordPress
- Delete link on single-custom.php with redirection
- How to structure a site with product variations pages?
- Change Permalink Structure for Tag.php template
- CPT: if more than X images are in post, use pagination
- $wpdb: Counting posts corresponding to 3 terms in 3 different taxonomies
- Why does querying on post_tags (which has been applied to custom post types) only return posts?
- Does anyone have a plugin that could automatically tag custom post types in wordpress?
- Is there a way to have the view link on manage posts page to open in a new window or tab?
- Getting rid of /my_custom_post_type/ in the single view URL
- Custom post type menu missing after 3.0b2 -> 3.1.2 upgrade
- Disable featured image
- How to display Custom Post Type Post based on Tag with Shortcode Parameter?
- Can’t query tag and post_type at the same time
- Tag page with Custom Post Types not returning any posts
- Custom Page that comes with preloaded content for the user
- Insert post metadata for all posts in CPT at once if metadata no existent
- How inefficient is it to use a Custom Post for 4-5 sentences & pull all to 1 page?
- get_attached_media() on author page not working
- How to check post type (to include custom css)
- How to customize a permalink (URL) structure?
- Plugin: register custom post types, child ready and performance best practices
- Convert simple SQL Query to WordPress query
- Display only one post each WEEK
- Custom post types and tag archive pages/permalink structure issue
- Tag Archive for Custom-Post-Type Posts yielding 404 when permalinks set to postname
- How to Mass Delete Images from Media Library
- Permalink structure by Post Meta value
- How to integrate video slide using custom post types?
- search also in taxonomy, tags and custom fields
- Custom post type getting wrong categories and tags
- A question on creating filters for custom posts using taxonomy
- Conditional Query of Custom Post Type and custom taxonomy
- will post_id ever change? Can I safely use post_id for custom queries?
- Listing tags from a custom post type
- List custom taxonomy specific to one custom post type
- Define new user capability for custom post types?
- How to allow visitors to enter custom post type?
- Why can’t my admin account manage attachments on a custom post type with a unique capability type?
- Is it possible to create relational metabox values in a custom post?
- Custom Post Type Query for Sidebar Doesn’t Work on Front Page
- Meta query for custom post type ignored in main query
- wp_update_term_count_now doesn’t work with a custom post type?
- Add tag to a custom post type doesn’t change the total of custom post type in backend
- Custom Tag Description unable to display just below and outside of the Loop
- 2 Templates 1 custom post type according url
- How do I move/order posts with a tag to the end?
- changing meta value and meta key of price field
- How can I get the number of custom post type posts that have a specific attachment image set?
- WordPress sort search results by custom order
- Use same slug base for Custom Post Type posts, and multiple taxonomy terms
- Hide parent categories when clicked, and show it’s childs
- Custom Path/Folder behind a custom post type
- Time based access control of custom post types – what is good approach?
- Show titles, date of all posts on single category page
- delete_published_posts does not work
- How to display “Category and Post_tag” component in a CPT Gutenberg edit screen?
- How to start a new post with custom Taxonomies already set?
- WooCommerce sort products by the actual product width(not the shipping width)
- Create a custom php page and load it at a specific slug
- Structure of data : CPT + terms
- How to handle paged param in post and custom-post-type?
- Default text in a specific post type, but not visible?
- Update postmeta Parent when post_status child change
- WP_Query custom post type query not showing the exact post type
- Assign External Database Queries to Global Variables and Make Them Accessible
- How to import a custom MySQL DB table into a WordPress custom post type
- Shortcode to display Staff post type based on Location post type and Specialty post type
- Hierarchy and access control for Custom Post Types (CPT)
- How to remove post listing page for a custom post type
- How to include term custom meta into the custom taxonomy term permalink structure
- Searching Custom Post Types
- How to Get WordPress custom post data in WooCommerce product meta panel? [closed]
- Function causing the loop display posting more (empty)
- A form that can save/edit after first input
- Adding the_content() in custom template email
- Cannot save CPT meta box
- non-hierarchical post type with hierarchical url structure
- Cross reference custom post types
- Send notification to the admin when new custom post is submitted
- How do i search authors from search form using author’s name
- Allow users to create posts without logging in?
- How to add attributes to taxonomies that may be different from post to post?