0

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:

they are in there!

4
  • What is the issue here ? This meta query will display results when for_buy_or_rent is buy AND postcode is 3066 ? What is your desired result ?
    – Sumit
    Commented Apr 28, 2016 at 4:05
  • Yes, that's correct. postcode doesn't even work by itself though. Commented Apr 28, 2016 at 5:21
  • Then change it to OR [relation] => AND that is on the beginning!
    – Sumit
    Commented Apr 28, 2016 at 5:28
  • if you're trying to get a postcode exact match, = is much faster than LIKE
    – Milo
    Commented Apr 29, 2016 at 4:52

1 Answer 1

0
if($search->keyword){
    $project_args['s'] = $search->keyword;
}

That was the culprit. FML!

Everything else was correct.

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.