Home » Wordpress » WordPress Meta query search

WordPress Meta query search

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have custom Field named “motor_brand” of posts.This meta values saved as ["honda","suzuki","kawasaki","toyota"]

If i search just honda by meta_query then all pos having honda meta value if iwant to search honda and kawasaki then the having honda or kawasaki meta values appear if i want to search kawasaki,honda and toyota then posts that having meta value honda, kawasaki or toyota can be appers.

I have set meta query like this

meta_ query = array(
     'relation'=>'OR'
       array(
        meta_key =>'motor_brand'
        value => "honda",
        compare => "LIKE"
        ),
       array(
        meta_key =>'motor_brand'
        value => "kawasaki",
        compare => "LIKE"
        ),
)

That works fine but when i try

meta_ query = array(
     'relation'=>'OR'
       array(
        meta_key =>'motor_brand'
        value => "honda",
        compare => "LIKE"
        ),
       array(
        meta_key =>'motor_brand'
        value => "kawasaki",
        compare => "LIKE"
        ),
       array(
        meta_key =>'motor_brand'
        value => "toyota",
        compare => "LIKE"
        ),
)

That not work

i want to search to many motor brands

Answers:

Maybe you want to try this:

$meta_query = array(
   array(
       'key' => 'motor_brand',
       'value' => array('honda','kawasaki','toyota'), 
       'compare' => 'IN',
   )
);

It should produce WHERE motor_brand IN ('honda','kawasaki','toyota') and should show you results if you execute on your db directly.

Edit

Oh sorry, i saw you wrote

I have custom Field named “motor_name” of posts.This meta values saved
as [“honda”,”suzuki”,”kawasaki”,”toyota”]

The IN doesnt work if you stored it like this. But what about motor_name. Your Field is named motor_name but your query asks for motor_brand.