20

First of all, I know it's a duplicate, but none of the older answers were helpful.

I'm searching in posts through post_meta. Here's my code, which currently returns nothing.

$args   =   array(
    'numberposts'   => -1,
    'post_type'     => 'post',
    'meta_query'    => array(
        array(
            'key'       => 'system_power_supply',
            'value'     => array('single', 'redundant'),
            'compare'   => 'IN',
        )
    )

);

$query = new WP_Query($args);
echo $query->found_posts;

If I remove meta_query it works. I'm sure of these things:

  • There's no spelling mistake in the key or the value.
  • post type is post
  • There is a post with the value 'single' in 'system_power_supply'. However, post fields are generated by Advanced Custom Fields.
3
  • is system_power_supply serialized?
    – Howdy_McGee
    Apr 3, 2015 at 19:52
  • yes, it is. Here's the value in postmeta table a:1:{i:0;s:6:"single";}
    – Rizwan
    Apr 3, 2015 at 20:20
  • 2
    well that's your problem, meta query won't work on serialized data. if you search this site for querying serialized data you'll find some answers, but no method will be ideal.
    – Milo
    Apr 3, 2015 at 20:30

2 Answers 2

21

There's no easy way to search serialized values in a meta query. If the list of values isn't crazy long, potentially you could set up multiple meta queries:

'meta_query'    => array(
    'relation' => 'OR',
    array(
        'key'       => 'system_power_supply',
        'value'     => 'single',
        'compare'   => 'LIKE',
    ),
    array(
        'key'       => 'system_power_supply',
        'value'     => 'redundant',
        'compare'   => 'LIKE',
    )
)

Or if you wanted to get super fancy, you could set it up dynamically:

$values_to_search = array('single', 'redundant');
$meta_query = array('relation' => 'OR');
foreach ($values_to_search as $value) {
    $meta_query[] = array(
        'key'       => 'system_power_supply',
        'value'     => $value,
        'compare'   => 'LIKE',
    );
}
7
  • Thank you so much, bro. I can't tell you how big headache you've solved of mine.
    – Rizwan
    Apr 3, 2015 at 20:52
  • btw, why the meta_value containing serialized data if we're not able to query through meta_query ? Is this wordpress's bug?
    – Rizwan
    Apr 3, 2015 at 20:53
  • 4
    I'm a woman, not a "bro", but no problem. The meta_value contains serialized data because of the way Advanced Custom Fields saves the data. It's not ideal, for sure.
    – Jen
    Apr 6, 2015 at 15:38
  • 1
    haha, I apologize my lady. Second and third worked well, didn't try the first one.
    – Rizwan
    Apr 6, 2015 at 16:14
  • 2
    you can remove the first one, it doesn't work
    – Toskan
    Mar 12, 2016 at 22:13
6

I know it's been a long time, but just in case someone has the same issue. Well i've been pulling my hair for hours before i found the issue: 'meta_query' with 'IN' comparison operator doesn't seem to accept the usual array. instead, you need to join it first with ', '.

So, in your case, something like this should work :

$args   =   array(
'posts_per_page'   => -1,
'post_type'     => 'post',
'meta_query'    => array(
    array(
        'key'       => 'system_power_supply',
        'value'     => join(', ', array('single', 'redundant')),
        'compare'   => 'IN',
    )
)
);
$query = new WP_Query($args);
echo $query->found_posts;
1
  • In WP 5 you can just pass the array directly to the value key. If you implode it to a string you may get unexpected results in regards to how wp splits the string up into segments for the IN() part. Eg, 'this that', 'and', 'that' becomes 'this','that','and','that' - so seems to be better to just give it the array. May 3, 2019 at 9:43

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.