-2

IN works quite fine for comparison though

   array(

                    'key' => 'customdata',

                    'value' => $arraydata,

                    'compare' => 'IN'

                ),

but when i try below with LIKE it returns strange result with long codes and also no record is found against it. Any help much appreicated

   array(

                    'key' => 'customdata',

                    'value' => $arraydata,

                    'compare' => 'LIKE'

                )

$loop = new WP_Query($loopArray);
echo "Query: {$loop->request}";
14
  • What is inside $arraydata? LIKE is used to compare strings. What values are you hoping to match with an array by using LIKE? Do you want to check LIKE for each element in the array? Commented Feb 3, 2020 at 11:01
  • PS: The strange long codes are normal. They are placeholders for %. See: wordpress.stackexchange.com/questions/303935/… Commented Feb 3, 2020 at 11:02
  • But it should return the result. I can get correct result through IN comparison operator where as LIKE is not returning any result though i have the data which is why IN brings exact matched values. Below is the full array code: $loopArray['meta_query'][] = array('relation' => 'OR',array('key' => 'customdata1','value' => 'MyVal1','compare' => 'LIKE'),array('key' => 'customdata2','value' => 'MyVal2','compare' => 'LIKE'));
    – Sam222
    Commented Feb 3, 2020 at 11:08
  • 1
    IN is for checking if a value is in an array. LIKE is for comparing strings. They are for different things. You can't just use them interchangeably. You've ignored my question: What is inside the $arraydata variable? Is it a string, or an array? Commented Feb 3, 2020 at 11:32
  • 2
    explode creates array. Array may not be used in LIKE comparison. Use for loop on this array to create multiple LIKE conditions for each possible value and combine them in meta_query array with relation => 'OR'. See wordpress.stackexchange.com/questions/104060/… for combined meta_query example. Commented Feb 3, 2020 at 17:23

1 Answer 1

3

You can't use LIKE to compare arrays. LIKE is used to check if a string matches, or partially matches, a value in the database.

IN is used to check if a value in the database is in a given set of values.

They are not interchangeable.

The correct comparison to use in your case depends entirely on what data you're using, and what you're trying to do, which is not clear from your question, or comments. But here's some pointers:

  • If you want to match a partial string, use LIKE.
  • If you want to match a value in an array of values, use IN.
  • If you want to match values between an array of two given values in, use BETWEEN.
  • If you want to match an exact string or number, use =.
  • If you want to match multiple partial strings in an array, you need to split that array into separate LIKE queries for each item in the array.

The "strange" query you are seeing is $wpdb 'escaping' the % placeholders used in LIKE queries. This is expected, and is part of a security feature added in 4.8.3. You can learn more at this answer.

1
  • Jacob, Thank you for the great help , issue is resolved.
    – Sam222
    Commented Feb 4, 2020 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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