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
|
Show 9 more comments
1 Answer
Reset to default 3You 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
版权声明:本文标题:mysql - WP_Query with LIKE returns strange query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744779829a2624640.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$arraydata
?LIKE
is used to compare strings. What values are you hoping to match with an array by usingLIKE
? Do you want to checkLIKE
for each element in the array? – Jacob Peattie Commented Feb 3, 2020 at 11:01%
. See: wordpress.stackexchange/questions/303935/… – Jacob Peattie Commented Feb 3, 2020 at 11:02IN
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:32for
loop on this array to create multiple LIKE conditions for each possible value and combine them in meta_query array withrelation => 'OR'
. See wordpress.stackexchange/questions/104060/… for combined meta_query example. – Vitauts Stočka Commented Feb 3, 2020 at 17:23