2

If I use get_posts() like so I get a number of results with the value 1 for the my_key meta_key:

$posts = get_posts( 
    array( 
        'post_type'  => 'attachment', 
        'meta_key'   => 'my_key', 
        'meta_value' => '1' 
    ) 
);

//this has a bunch of results as expected
print_r($posts);

However if I create a similar query with WP_Query I get an empty result array

$args = array(
   'post_type' => 'attachment',
   'meta_query' => array(
       array(
       'key'     => 'my_key',
       'value'   => '1',
       'compare' => '=',
       'type'    => 'BINARY'

      )
   )
);

$query = new WP_Query();
$results = $query->query($args);
//this is empty
print_r($results);

I have tried a few varieties of the meta_query array all with no luck. I am thinking that this might be a bug, but wanted to make sure I was not missing something first.

8
  • Did you forget the ' after value in your actual code, or is that a WPSE specific typo?
    – mor7ifer
    May 15, 2012 at 12:59
  • No, but thanks I was wonder why the code was not formatting.
    – Mike
    May 15, 2012 at 13:04
  • What happens if you drop compare and type completely?
    – mor7ifer
    May 15, 2012 at 13:06
  • following @m0r7if3r: what happens if you change BINARY to NUMERIC ?
    – moraleida
    May 15, 2012 at 13:09
  • Dropping the compare and type does not change the outcome... Numeric is also a no go
    – Mike
    May 15, 2012 at 13:12

2 Answers 2

12

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'.

3
  • See my update. This updated code is tested to work.
    – EAMann
    May 16, 2012 at 14:00
  • shouldn't the post_status be publish?
    – Mike
    May 16, 2012 at 15:00
  • 2
    Default value is publish, but take a look in your database, the post status for attachments is "inherit."
    – EAMann
    May 16, 2012 at 15:17
0

I believe your problem is that you're trying to use WP_Query like get_posts(). It seems quite possible that the query is working, but you just can't see the results. WP_Query returns a query object that you loop through like this:

...
$my_query = WP_Query( $args );
while( $my_query->have_posts() ) : $my_query->the_post();

// do your thing

endwhile;

Also, note that I used $my_query. I'm a little fuzzy on this, but I believe $query may be a reserved variable from WordPress, and either way, it's often better if you make that query a little-more human-readable (maybe event $attachment_meta_query or something).

2
  • Thanks, but your code is effectively the same as mine. You are just taking a short cut. So this does not solve the problem. I really think we may have a bug on our hands.
    – Mike
    May 15, 2012 at 15:18
  • 2
    That's not a shortcut, but rather the proper syntax for invoking a new WP_Query(). Pass the $args array directly to WP_Query(). Also: I strongly recommend using more-descriptive variable names, in order to avoid naming conflicts. May 15, 2012 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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