23

I have a gallery attached to a page. On that page, I'm running the following query:

$events_gallery = new WP_Query( // Start a new query for our videos
array(
    'post_parent' => $post->ID, // Get data from the current post
    'post_type' => 'attachment', // Only bring back attachments
    'post_mime_type' => 'image', // Only bring back attachments that are images
    'posts_per_page' => '3', // Show us the first three results
    'status' => 'inherit', // Inherit the status of the parent post 
    'orderby' => 'rand', // Order the attachments randomly  
    )
);

I've experimented quite a few ways and, for some reason, I can't get attachments to return. Am I missing something obvious here?

Update*

Thanks to Wok for pointing me in the right direction.

It turns out I was using "status" instead of "post_status". The codex had used "status" as the example in its in-context explanation of the "attachment" post type. I updated the codex to reference "post_status" instead. The correct code is as follows:

$events_gallery = new WP_Query( // Start a new query for our videos
array(
    'post_parent' => $post->ID, // Get data from the current post
    'post_type' => 'attachment', // Only bring back attachments
    'post_mime_type' => 'image', // Only bring back attachments that are images
    'posts_per_page' => '3', // Show us the first three results
    'post_status' => 'inherit', // Attachments default to "inherit", rather than published. Use "inherit" or "any".
    'orderby' => 'rand', // Order the attachments randomly  
    )
);  
2
  • I wonder what the difference is between post_status being set to 'null' vs 'inherit''
    – Wok
    Commented Apr 20, 2011 at 17:29
  • 4
    You just saved me a lot of pain with 'post_status' => 'inherit' Thanks!
    – Pat
    Commented May 3, 2011 at 16:19

4 Answers 4

19

These are the query parameters i use...works for me when i loop through the results

array(
    'post_parent' => $post->ID,
    'post_status' => 'inherit',
    'post_type'=> 'attachment',
    'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png'                  
);

For more detail, please see official documentation for WP_Query's status parameters

0
20

Add in $args, it is important.

'post_status' => 'any'

Do not: 'post_status' => null

This is important because attachments don't have a post_status, so the default value for post_status, published, will find no attachments.

2
  • Please make an effort to explain the answer, rather than just post a line or two of code.
    – s_ha_dum
    Commented Sep 19, 2013 at 6:27
  • Yes, how come this works? I couldn't get my attachment to show up in my archive page until I added this.
    – Claire
    Commented Apr 26, 2015 at 8:56
0

Looking at the query it generates, it does appear to be a bug of sorts. 'status' => 'inherit' is interpreted as the parent's status, when the entry in the db for the attachment is literally 'inherit'.

An alternative is to use get_children in place of WP_Query.

0

I've been able to display all images that are attachments to a post using this code.

<?php
$args = array( 'post_type' => 'attachment', 'orderby' => 'menu_order', 'order' => 'ASC', 'post_mime_type' => 'image' ,'post_status' => null, 'post_parent' => $post->ID );
$attachments = get_posts($args);
    if ($attachments) {
    foreach ( $attachments as $attachment ) { ?>
      <img src="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" />
<?php   }
    } ?>

And to echo the URL of the original full size image, you could link that image off to

<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>

Hopefully this is an approach to what you're trying to do.

2
  • Does the pagination work with that? And can you show the rest of your output code? I'm in the middle of re-coding a themes gallery to actually paginate the attachments that are on the page. Thanks!
    – user4805
    Commented Apr 20, 2011 at 22:31
  • If I upload 4 images to a post, and I add this into the main content entry div in single.php, it will just spit out 4 image tags. The src= of each will lead to the original large image size. Pagination does not work with this, as it spits out any and all images that are attached to the post. Commented Apr 20, 2011 at 22:45

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.