I am absolutely boggled by this one.
I am creating a search function for a property site - simple enough. Simple queries such as max_price
, max_beds
, rent_or_buy
work ok. But I am trying to create a keyword match search that can query postcode
, project_id
and suburb
my query comes up empty.
My meta_query
argument of WP_Query
looks like this;
(
[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
)
)
)
note - I use a switch case to build it on the fly.
Running $projects->request outputs the following 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
If I get rid of the sub-query OR
, it works. If I try to use one of the sub queries on the top level AND
query, it doesn't match. Check a screenshot of my database to ensure the match is there:
for_buy_or_rent
isbuy
ANDpostcode
is3066
? What is your desired result ?postcode
doesn't even work by itself though.[relation] => AND
that is on the beginning!=
is much faster thanLIKE