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
- Restrict custom post type to only site administrator role
- How to force one column layout on custom post type edit page?
- How to change “Draft” string for status of custom post type to “Unavailable”?
- get attachments for all posts of particular post type
- Search multiple custom fields by using meta_query
- How to Remove Certain Screen Options and Table Columns from post type in wp_list_table?
- post_tag taxonomy with custom post type
- Redirect after deleting post and keep track of pagination
- Add comments meta fields to comments metabox on post edit screen
- remove_action conditionally for Custom Post Type
- Assigning a role to a specific custom post type (and ignoring other post types)
- Display posts in random post types
- Combine tax_query and meta_query in WP_Query
- Do attachments added to custom post types have a post parent?
- Registering tags taxonomy for a custom post type
- Categories and Tags not working!
- How do I add a custom button to my “edit” list? ( edit.php?post_type= ) beside “Add New”
- How to get Custom Post ID by adding filter to child theme’s function
- Remove All, Published and Trashed Post Views in Custom Post Type
- Can custom taxonomies items have attached properties?
- WordPress if in term*
- Weird problem happening with custom taxonmy when creating/updating posts
- wpdb custom post_type problem
- CPT: if more than X images are in post, use pagination
- $wpdb: Counting posts corresponding to 3 terms in 3 different taxonomies
- Does anyone have a plugin that could automatically tag custom post types in wordpress?
- How to display Custom Post Type Post based on Tag with Shortcode Parameter?
- Custom post types and tag archive pages/permalink structure issue
- Tag Archive for Custom-Post-Type Posts yielding 404 when permalinks set to postname
- Conditional Query of Custom Post Type and custom taxonomy
- Structure of data : CPT + terms
- Default text in a specific post type, but not visible?
- 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
- Cross reference custom post types
- How do you get the full size url of an attachment image
- Show all tags on custom post type
- Ordering Submenu Pages in WP 3.1
- How to customize work area / admin area in a custom post type without plugins?
- Change “empty trash” button text?
- Meta data (Tags and Categories) for Custom Posts not showing.
- Front end access to a SQL data repository – recommended way?
- How to display custom field value on page?
- get_the_tag_list() returns bad links
- Move Genesis Single Page/Single Post Title
- How can I create an automatic drop down menu with my tags?
- why post_class() function apply css classes to all other files?
- Custom post type’s posts are not showing anywere but in xml sitemap
- Only show columns in custom post type?
- Display Custom Post Types Glossary
- How to constrain the results to a specific post_type on tag archive page?
- New post from database
- How should I structure complex content hierarchies?
- How to Display the tag content of Custom Post?
- Convert Custom Table Data Based Plugin To Be Searchable
- Convert Custom Taxonomy Posts to Normal Posts
- Problems making shortcode with custom post types and taxonomy
- Issue with Custom Post Type Tags
- Query Multiple Custom Post Types & Exclude a Taxonomy Term
- Problem with customize page cached in WordPress.com [closed]
- Multiple categories assigned to a single product breaking the breadcrumb
- Adding a location field to buddypress activity
- How can I assign multiple parents to CPT?
- get taxonomies from terms
- Custom post type and custom taxonomy key lenght & query performance
- How to convert Post Object Custom Field into Tags
- Getting 404 on child page with pre_get_posts() on custom posts
- Sql Update CPT from publish to draft and particular custom field
- Dropwdown: Get taxonomy terms from posts that share preselected terms from 3 other taxonomies
- Limit posts per page depending on the size of a div?
- Delete Post by User
- save_post affect creation and deletion
- Set a static page as a user profile page?
- Problem to get the link of the default ‘post’ post type like the orther custom types
- How to display all custom fields associated with a post type – IN THE ADMIN AREA?
- How to keep a CPT stick to specific position?
- WP_Query for CPT with filter by another WP_Query
- Default taxonomy ‘post_tag’ added to CPT managing by custom role : nothing in the metabox
- Problems with a custom meta_box
- Multiple Block Quotes without using HTML
- How to apply order on custom taxonomy and custom meta key on custom post type
- How to make a template for a specific post of a custom post type?
- Bulk update custom post types
- ACF select box css color change
- Can WordPress show posts based on a button that the user clicked 2 pages back?
- Creating post custom field text area
- Delete old thumbnail when updating new