I am trying to retrieve attachments that are attached to my current post. This is the code that I am trying to use. What I'm attempting to do is query all of my posts that are of custom post type business-manager. From within that loop I am starting another query to retrieve the attachments associated with that post. If I var dump the $attachment variable I get an empty array. If I pass the array for attachment variable into get_posts($args) then do a var dump I can retrieve all of the images. How can I do this using a new WP_Query object?

$my_query = new WP_Query( array(
           'post_type'         =>  'business-manager',
           'posts_per_page'    =>  1,
           'tax_query'         =>  array(
               'taxonomy'      =>  'business-type',
               'field'         =>  'slug',
               'terms'         =>  'featured'
    ) );

$attachment = new WP_Query( array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' => null,
    'post_parent' => $my_query->post->ID
) );
  • Without further tests … did you try $my_query->posts[0]->ID?
    – fuxia
    Commented Dec 27, 2012 at 21:40

1 Answer 1


From get_media_items function and his code, you can use get_children adding mime type for images:

$images = get_children( array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') );

That will get you an array ($images) of images attached to $post_id. In you case, if you're inside a loop, $post_id should be equal to $post->ID

Not exactly a WP_Query implementation as you asked for, but it's easy.

If you still need the WP_Query way, you can use your own code i think (not tested). Inside a loop: $attachment = new WP_Query( array( 'post_parent' => $post->ID 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_mime_type' => 'image' ) );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.