这件事让我大吃一惊。
我正在创建一个物业网站的搜索功能-非常简单。简单查询,如max_price
, max_beds
, rent_or_buy
工作正常。但我正在尝试创建一个关键字匹配搜索,可以查询postcode
, project_id
和suburb
我的查询结果为空。
我的meta_query
的参数WP_Query
看起来像这样;
(
[relation] => AND
[0] => Array
(
[key] => for_buy_or_rent
[value] => buy
[compare] => =
[type] => CHAR
)
[1] => Array
(
[relation] => OR
[0] => Array
(
[key] => project_id
[value] => 3066
[compare] => LIKE
[type] => NUMERIC
)
[1] => Array
(
[key] => postcode
[value] => 3066
[compare] => LIKE
[type] => NUMERIC
)
[2] => Array
(
[key] => suburb
[value] => 3066
[compare] => LIKE
[type] => CHAR
)
)
)
注意-我使用开关盒动态构建它。
运行$projects->request输出以下SQL;
SELECT SQL_CALC_FOUND_ROWS wp_posts.id
FROM wp_posts
INNER JOIN wp_postmeta
ON ( wp_posts.id = wp_postmeta.post_id )
INNER JOIN wp_postmeta AS mt1
ON ( wp_posts.id = mt1.post_id )
WHERE 1 = 1
AND (( ( wp_posts.post_title LIKE \'%3066%\' )
OR ( wp_posts.post_excerpt LIKE \'%3066%\' )
OR ( wp_posts.post_content LIKE \'%3066%\' ) ))
AND ( ( wp_postmeta.meta_key = \'for_buy_or_rent\'
AND Cast(wp_postmeta.meta_value AS CHAR) = \'buy\' )
AND ( ( mt1.meta_key = \'project_id\'
AND Cast(mt1.meta_value AS signed) LIKE \'%3066%\' )
OR ( mt1.meta_key = \'postcode\'
AND Cast(mt1.meta_value AS signed) LIKE \'%3066%\' )
OR ( mt1.meta_key = \'suburb\'
AND Cast(mt1.meta_value AS CHAR) LIKE \'%3066%\' ) ) )
AND wp_posts.post_type = \'project\'
AND ( wp_posts.post_status = \'publish\'
OR wp_posts.post_status = \'acf-disabled\'
OR wp_posts.post_status = \'future\'
OR wp_posts.post_status = \'draft\'
OR wp_posts.post_status = \'pending\'
OR wp_posts.post_status = \'private\' )
GROUP BY wp_posts.id
ORDER BY wp_posts.post_title LIKE \'%3066%\' DESC,
wp_posts.post_date DESC
LIMIT 0, 24
如果我去掉子查询
OR
, 它起作用了。如果我尝试在顶层使用其中一个子查询
AND
查询,它不匹配。检查我的数据库的屏幕截图以确保存在匹配项: