0

I apologize beforehand for any confusion, I'm just 2 days with WP.

So, I use Advanced Custom Fields to add formatable custom fields to posts of partucular type. One of fields is checkboxes with multiple choice with name spec. Now I want to get all posts where spec has one or more values checked.

For example: choices for this field are {"choice1","choice2",choice3","choice4","choice5"} In some post I selected "choice2" and "choice3" and saved these posts.

if I print spec in loop

print_r( get_post_meta($post->ID,'spec',true));

it can looks like:

Array
(
    [0] => choice2
    [1] => choice3
)

Now I want to get all posts where "choice2" or "choice3" are selected (or may be only choice2 or only choice3):

query_posts(
    array(
        'cat' => 6,
        'posts_per_page'=>-1,
        'meta_query' => array(
            array(
                'key' => 'spec'
                'value' => array('choice2','choice3'),
                'compare' => 'IN'
            )
        )
    )
);

But that not works. As I understand the real condition will be WHERE spec IN 'choice2', where choice is array. But I need opposite condition - something like in_array('choice2',spec) || in_array('choice3',spec). May be there is some HAS value for compare?

So my question - how can I get all the posts where meta field with multiple choice has several values checked?

1 Answer 1

2

did you try to call like this:

$args = array(
  'post_type'  => 'product',
  'meta_query' => array(
    'relation' => 'OR',
    array(
        'key'     => 'spec',
        'value'   => 'choice2',
        'compare' => 'LIKE',
    ),
    array(
        'key'     => 'spec',
        'value'   => 'choice3',
        'compare' => 'LIKE',
    ),
  ),
);
$query = new WP_Query( $args );

example taken straigh from the codex

5
  • Thanks, @peteroak, that works! Probably I was baffled by the fact that LIKE is applies more to strings than to arrays.
    – folibis
    Commented Apr 21, 2015 at 4:19
  • glad i could help! :D
    – pcarvalho
    Commented Apr 21, 2015 at 15:19
  • Now I noticed that it work not as I expected. for example if I have choices {"aa","bb","aacc"} and aa is criteria for searching so it returns also posts where only aacc was selected. In other words it works like a real LIKE but I need it works like in_array
    – folibis
    Commented Apr 22, 2015 at 2:31
  • Ok, after a bit of research I've found that WP loop does not get meta data in account while SQL query. It can be easy shown by echo $GLOBALS['wp_query']->request. As I understand WP receives all the query records without meta filtering (OMG!) and apply this filter in loop itself for each record. So I removed all meta filter from query_posts to do that by myself, in code with in_array.
    – folibis
    Commented Apr 22, 2015 at 6:08
  • nice insight. in the case above, you could use %aa% to get xxaabb. you should write your own answer too below ;)
    – pcarvalho
    Commented Apr 23, 2015 at 1:11

Your Answer

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

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