1

I'm trying to include post meta in the scope of a WordPress search using WP_Query.

I have a meta query that looks like this:

$args['meta_query'] = [
    'relation' => 'AND',
    [
         'key' => 'quantity',
         'value' => 1,
         'compare' => '>='
    ],
    [
        'relation' => 'OR'
        [
            'key' => 'brand',
            'value' => 'bar'
        ],
        [
            'key' => 'vendor',
            'value' => 'biz'
        ]
    ]
]

When I add the 's' parameter to the query object, it seems like it's matching posts that have one of the meta query fields "brand" or "vendor", but also matches the description or title.

I want it to match like this: brand OR vendor OR title OR description.

The current semantics seem to be giving me something more like: (brand OR vendor) AND (title OR description)

1
  • What you want isn't possible just through just WP_Query arguments. You'd need to filter the SQL directly, with posts_where or posts_clauses. Aug 29 '18 at 3:07
0

I had the same desire. A quick and dirty answer is to use the get_meta_sql to replace the leading 'AND' with an 'OR'.

(See also the answer here: https://stackoverflow.com/questions/43734845/how-to-give-or-relation-in-tax-query-and-meta-query-wordpress ( A related, but not duplicate, question.) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.