Wordpress - meta_query 'compare' => 'IN' not working
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',
);
}
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;