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
I don’t know if you’ve tried this but try adding
post_status => inherit
to your query. I’ve found thatget_posts
defaults topublish
and therefore will not return any attachments, since theirpost_status
is alwaysinherit
. The codex page onget_posts
confirms this.Edit: looking at your query again, you have
term
in thetax_query
where it should beterms
: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters