0

I am trying to build a query that returns all posts, with a meta value that exists in a set of values.

My posts have a meta key called Asin_unique and I want to find all posts, that have the value B006MWDNVI,B00BCMCIS2, or B01ARRJFGA in this field.

This is what I have built so far, but it only returns me the post with B00BCMCIS2, not the other ones. What do I have to change to make it work?

$args  = array(
    'post_status' => 'any',
    'post_type'   => 'any',
    'numberposts' => -1,
    'meta_query'  => array(
        array(
            'key'     => 'Asin_unique',
            'value'   => '(B006MWDNVI,B00BCMCIS2,B01ARRJFGA)',
            'compare' => 'IN',
        )
    )
);

$posts = get_posts( $args );
2

Check This Will Work for you.

$args  = array(
    'post_status' => 'any',
    'post_type'   => 'any',
    'numberposts' => -1,
    'meta_query'  => array(
        array(
            'key'     => 'Asin_unique',
            'value'   => array('B006MWDNVI','B00BCMCIS2','B01ARRJFGA'),
            'compare' => 'IN',
        )
    )
);

$posts = get_posts( $args );

Help Link: https://rudrastyh.com/wordpress/meta_query.html

  • Thank you. But this didn't work either. You got me on the right track though, that it has to do something with how the value is formatted. I fooled around with it a bit: If I just leave out the brackets in my example, it seems to work. – TheKidsWantDjent Apr 23 '18 at 12:05
0

Leaving out the brackets in the value seems to do the trick. I was mislead by how IN is usually formatted in SQL and thought it would be the same with WP queries, but you have to leave them out:

$args  = array(
    'post_status' => 'any',
    'post_type'   => 'any',
    'numberposts' => -1,
    'meta_query'  => array(
        array(
            'key'     => 'Asin_unique',
            'value'   => 'B006MWDNVI,B00BCMCIS2,B01ARRJFGA',
            'compare' => 'IN',
        )
    )
);

$posts = get_posts( $args );

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.