php – ACF meta_query comparing an array to an array not working

Question

I am trying to create a product filter for my site, I am using an ACF text field called color which stores an array of colors associated with the product and I am trying to filter the products by an array of selected color or colors from a checkbox group(multiple color checkboxes can be selected at once). Here’s an example of what I tried for the meta_query:

if (isset($_POST['color'])) {
  $meta_query[] = array(
    'key'     => 'color',
    'value'   => $_POST['color'],
    'compare' => 'IN'
  );
}

Let’s assume that in this example $_POST['color'] returns this:

Array(
    [0] => green
    [1] => blue
)

In my products post type I have a product which has this in the color ACF field:

["green","yellow"]

If I use my code above using IN as the compare operator I get no results. If I change the operator to LIKE I get results, but it shows me all the products, even if the product does not include green or blue in the color array of the ACF field. Any ideas where I’m going wrong?

Thanks!

0
user13286 1 month 2021-08-20T11:44:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse