admin管理员组

文章数量:1392116

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}";

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}";
Share Improve this question edited Feb 3, 2020 at 11:00 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Feb 3, 2020 at 10:52 Sam222Sam222 1 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? – Jacob Peattie Commented Feb 3, 2020 at 11:01
  • PS: The strange long codes are normal. They are placeholders for %. See: wordpress.stackexchange/questions/303935/… – Jacob Peattie 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? – Jacob Peattie 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/questions/104060/… for combined meta_query example. – Vitauts Stočka Commented Feb 3, 2020 at 17:23
 |  Show 9 more comments

1 Answer 1

Reset to default 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.

本文标签: mysqlWPQuery with LIKE returns strange query