1

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) {
  ["tax_query"]=>
  array(1) {
    [0]=>
    array(3) {
      ["taxonomy"]=>
      string(8) "category"
      ["field"]=>
      string(4) "slug"
      ["term"]=>
      array(1) {
        [0]=>
        string(10) "javascript"
      }
    }
  }
  ["numberposts"]=>
  int(-1)
  ["orderby"]=>
  string(10) "menu_order"
  ["order"]=>
  string(3) "ASC"
  ["post_status"]=>
  string(7) "inherit" // 'any' also tried
  ["post_type"]=>
  string(10) "attachment"
  ["post_mime_type"]=>
  string(34) "application,video,text,audio,image"
  ["post_parent"]=>
  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:

SELECT SQL_CALC_FOUND_ROWS  cms_posts.ID 
FROM cms_posts  
WHERE 1=1  
AND 0 = 1 
AND (
    cms_posts.post_mime_type 
    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 
ASC LIMIT 0, 10
  • Does removing the taxonomy query return any results? – Manny Fleurmond Jul 21 '13 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? – Manny Fleurmond Jul 21 '13 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 '13 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 '13 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 '13 at 22:05
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 '13 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 '13 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.