I have seen various ways to do this but none working 100% for me. I have a dropdown/select menu and users can choose multiple choices. This then sends an array of values that I want to query.
Either I can query each one individually but this isn't realistic because the array has many values and I would have to then cater for every single option eg:
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'location',
'value' => 'Melbourne',
'compare' => 'LIKE'
),
array(
'key' => 'location',
'value' => 'Sydney',
'compare' => 'LIKE'
)
)
I also tried to serialise the array like below, but this only works if I choose one option from the select menu. As soon as select multiple options I get an empty array as the response.
'value' => serialize( $locations ),
'compare' => 'LIKE'
Finally, I tried a regex example I found but couldn't get that going either.
$field_value = sprintf( '^%1$s$|s:%2$u:"%1$s";', $locations, strlen( $locations ) );
'value' => $field_value,
'compare' => 'REGEXP'
array( 'Melbourne', 'Sydney' ), then'value' => '"(Melbourne|Sydney)"', 'compare' => 'REGEXP'would work. But then have you considered using a custom taxonomy namedlocationinstead?