0

I want to add multiple values in the meta_query. I found the soulution in this link:

meta_query with multiple values

Here is the code:

$meta_query_args = [
    [
        'key' => 'checkboxes',
        'value' => [ '"facebook"', '"twitter"' ],
        'compare' => 'LIKE',
    ],
];

$args = array(
    'post_type' => 'post' ,
    'meta_key' => $meta_value,
    'orderby' => $sort . ' date',
    'order' => $order_sort,
    'posts_per_page' => 10,
    'cat' => $category,
    'paged' => $paged,
    'meta_query' => $meta_query_args,   

);

But I have the following error:

Warning: trim() expects parameter 1 to be string, array given in ...

I would appreciate any help.

Thank you.

1

LIKE as a compare value doesn't make sense with an array. You want compare to be IN. Also, you've got an extra set of quotes in the values for no reason:

$meta_query_args = [
    [
        'key'     => 'checkboxes',
        'value'   => ['facebook', 'twitter'],
        'compare' => 'IN',
    ],
];

EDIT: So, according to your comment you're using ACF, which stores multiple checkboxes as a serialized array. Therefore you need to use multiple LIKE queries on individual strings.

$meta_query_args = [
    'relation' => 'OR',
    [
        'key'     => 'checkboxes',
        'value'   => '"facebook"',
        'compare' => 'LIKE',
    ],
    [
        'key'     => 'checkboxes',
        'value'   => '"twitter"',
        'compare' => 'LIKE',
    ],
];

Your mistake was trying to use an array as a value with LIKE. To query LIKE multiple possible values you need multiple meta queries with the OR relation. The extra quotes are because it's the only way to really query a specific value within a serialized array.

The meta value for checkboxes if Facebook and Twitter were checked would look something like this:

a:2:{i:0;s:8:"facebook";i:1;s:7:"twitter";}

So when you query LIKE '"facebook" you're trying to find the value "facebook" within a full value like that. The quotes are to make sure you don't incorrectly find a value like not_facebook.

If you have a lot of values that you need to query this way then your performance is going to be awful. This is a very inefficient way to store data for this type of query.

  • Thank you for your suggestion. I'm using ACF checkbox fields for that key. With your suggestion there is no error, but no results also. – Ante Medic Mar 18 '18 at 12:09
  • I've updated my answer. ACF stores multiple checkboxes in a specific way that requires a different approach. – Jacob Peattie Mar 18 '18 at 12:22
  • Thank you very much for your answer. And you are on top with every suggestion. In my original code I'm doing the way you have suggested. I have 6 options in checkbox, so that would be 6 arrays in meta_query. And I'm very concerned if it will slow down my site. – Ante Medic Mar 18 '18 at 12:36
  • Yes 6 LIKE queries in a meta query is almost certainly going to have terrible performance. Have you considered a taxonomy? – Jacob Peattie Mar 18 '18 at 12:37
  • 1
    thank you. I have created custom taxonomy and change my query. – Ante Medic Mar 18 '18 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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