Skip to main content
2 of 3
Updated with the answer.
Jonathan Wold
  • 1.7k
  • 3
  • 24
  • 47

Broken? WP_Query and "attachment" as a post type

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 result
    'post_status' => 'inherit', // Attachments default to "inherit", rather than published. Use "inherit" or "all". 
    'orderby' => 'rand', // Order the attachments randomly  
    )
);  
Jonathan Wold
  • 1.7k
  • 3
  • 24
  • 47