2

What is the best way to create a meta query that searches through ALL meta fields (keys)? What I'm trying to do is something such as:

$posts = new WP_Query(array(
    'meta_query' => array(
        'relation' => 'OR'
        array(
            'key' => ALL,
            'value' => keyword
        )
    )
));

Does that make sense? What is the proper way of doing it?

Thanks

1
  • 1
    Maybe, just maybe if you remove key and add compare => 'LIKE' - but there isn't a "select all" or wildcard for keys in meta_query.
    – Howdy_McGee
    Commented Jun 13, 2016 at 18:36

1 Answer 1

5

As @Howdy_McGee suggested and I made a quick test. Removing key do the trick so you can just remove the key and add compare LIKE if you do not want exact match.

Example:-

$posts = new WP_Query(array(
    'meta_query' => array(
        array(
            'value' => 'meta_value'
        )
    )
));

This will produce the SQL like

WHERE 1=1  AND ( CAST(wp_postmeta.meta_value AS CHAR) = 'meta_value' ) 

That is what we want!

2
  • 3
    Just remember, LIKE comparisons are quite expensive Commented Jun 13, 2016 at 18:47
  • 1
    Yes 100% true and also you may get undesired slow results because matching all the keys including the hidden one :)
    – Sumit
    Commented Jun 13, 2016 at 18:54

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.