0

I need your help with multiple values in meta_query. I have a checkbox filter. This is the query I came up:

But, I'm worried that query is not optimized, and that it will slow down the site.

I read the solution asked in this question: meta_query with multiple values But, when I implement that solution I have the following error: "Warning: trim() expects parameter 1 to be string".

Is there any way how to optimize this query?

I will appreciate any help.

Thank you.

$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' => array(
        'relation' => 'OR',
        array(
            'key' => 'checkboxes',
            'value' => $face,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'checkboxes',
            'value' => $twitter,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'checkboxes',
            'value' => $telegram,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'checkboxes',
            'value' => $reddit,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'checkboxes',
            'value' => $email,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'checkboxes',
            'value' => $phone,
            'compare' => 'LIKE'
        ),
    )
);
  • Where are the variables you’re using for value coming from? $face, $reddit etc. – Jacob Peattie Mar 18 '18 at 2:54
  • They are coming from the checkbox click. With Advanced Custom Fields plugin, I have added field type checkbox for the post. If user click on the checkbox in example for the facebook, field $face = 'facebook'. – Ante Medic Mar 18 '18 at 9:07
  • No, I mean where are you setting the value of the variables. Variables don't set themselves. Where in your code are you setting $face = . – Jacob Peattie Mar 18 '18 at 9:15
  • Here is the code for setting the variable $face if ( isset($_GET['f']) && $_GET['f'] == '1') {$face='"facebook"';} – Ante Medic Mar 18 '18 at 9:41
  • So if $_GET['f'] isn't set then $face isn't set. This is probably why you're getting the error. – Jacob Peattie Mar 18 '18 at 10:00
2

Post meta data was never designed to be used efficiently in queries. Using it with LIKE based matching will only make it more horrible.

You need to rethink your DB. For example use taxonomy terms to indicate which social network is associated with a post.

1

Without some other info it's pretty tough to give any solution to your this above issue. But it seems like your $face, $twitter, $telegram etc. data are coming in a form of object or other datatype(not array or string). But meta_query's value only accepts string and array. So, based on this assumption I would suggest you to make sure that all those data of yours should be a one dimensional array of the values you want to filter or simple string.

Hope that helps.

  • Hello, and thank you for the reply. Data from the $face, $twitter etc are coming from the checkbox click. If checkbox is checked $face = 'facebook'. – Ante Medic Mar 18 '18 at 9:10

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.