I'm working on a project that requires searching for attachments with a given taxonomy-slug pair (in this case, category=javascript). I'm using get_posts() with tax_query set to achieve this, but it is returning zilch.

The array being passed to get_posts() is the following:

array(8) {
  array(1) {
    array(3) {
      string(8) "category"
      string(4) "slug"
      array(1) {
        string(10) "javascript"
  string(10) "menu_order"
  string(3) "ASC"
  string(7) "inherit" // 'any' also tried
  string(10) "attachment"
  string(34) "application,video,text,audio,image"
  string(0) ""

There is an image in category JavaScript (which has javascript as the slug), but it is not being returned.

Also, on the off chance it's relevant, category is being set through use of Media Categories.

EDIT: post_status=>inherit and post_status=>any were tried with no change in the end result.

EDIT 2: Creating a new WP_Query object, leaving out the meta_query creates valid SQL, but including meta_query adds this: AND 0 = 1, which obviously makes the entire query return nothing. The entire SQL generated is below:

FROM cms_posts  
WHERE 1=1  
AND 0 = 1 
    LIKE 'application/%' 
    OR cms_posts.post_mime_type 
    LIKE 'video/%' 
    OR cms_posts.post_mime_type 
    LIKE 'text/%' 
    OR cms_posts.post_mime_type 
    LIKE 'audio/%' 
    OR cms_posts.post_mime_type 
    LIKE 'image/%')  
AND cms_posts.post_type = 'attachment' 
AND (cms_posts.post_status <> 'trash' 
    AND cms_posts.post_status <> 'auto-draft' 
    AND cms_posts.post_status <> 'spam') 
GROUP BY cms_posts.ID 
ORDER BY cms_posts.menu_order 
  • Does removing the taxonomy query return any results? Jul 21, 2013 at 18:19
  • Just noticed: you said that the taxonomy was a category but the query above is looking for a post tag. Which one is it supposed to be? Jul 21, 2013 at 18:26
  • Removing the taxonomy query returns all attachments meeting the rest of the query. And sorry about the post_tag. Mistakenly made it in when I was including the post_status.
    – Dan
    Jul 21, 2013 at 20:41
  • it's probably easier to debug using wp_query rather than get_posts, then you can inspect the query object and see the actual SQL being generated and sent to the database
    – Milo
    Jul 21, 2013 at 20:53
  • @Milo Well, just modified the original question with the results of creating a WP_Query with the attributes listed. Even more baffling than before.
    – Dan
    Jul 21, 2013 at 22:05

1 Answer 1


I don't know if you've tried this but try adding post_status => inherit to your query. I've found that get_posts defaults to publish and therefore will not return any attachments, since their post_status is always inherit. The codex page on get_posts confirms this.

Edit: looking at your query again, you have term in the tax_query where it should be terms: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

  • Logical suggestion, but didn't change anything. Also tried post_status=>any, what the get_posts codex entry uses in one of the attachment queries.
    – Dan
    Jul 21, 2013 at 13:43
  • Well I'll be buggered... You're correct. After pluralizing term to terms, everything is working how I would expect. Thanks!
    – Dan
    Jul 22, 2013 at 2:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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