First, just pass your arguments to the constructor of WP_Query
as this is both cleaner, and the way you’re supposed to do it according to the Codex documentation of the class.
You should be constructing things like this:
$my_key_query_args = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'meta_query' => array(
array(
'key' => 'my_key',
'value' => '1',
'compare' => '=',
'type' => 'BINARY'
)
)
);
$my_key_query = new WP_Query( $my_key_query_args );
Second, notice the added post_status
parameter of my array. By default attachments are added with a post status of “inherit,” but WP_Query
will look for posts with the status of “published,” “draft,” or “pending.” (See the documentation of that parameter as well).
So there’s no bug here, we just forgot to check the defaults for all parameters passed into the object.
There’s a note on the “attachment” option for the post_type
parameter that calls out this requirement:
The default WP_Query sets
'post_status'=>'published'
, but attachments default to'post_status'=>'inherit'
so you’ll need to set the status to'inherit'
or'any'
.