My question is very similar to Meta Query with AND & OR? . This question was answered in 2011 by @Otto.

With the exception that my logic is different. I want to query, for example, posts with custom meta that match submitted form. The two required bits are city and state. Therefore, my query must be of the 'AND' type.

However, I also want to query posts with custom data of 'metro-area' that matches the same city, if they have that data. Not all posts do. This yields the need for an additional query, but of type 'OR', considering that I don't care if they match against the 'city' field, just the 'metro-area'.

Real world scenario being that if I have small cities within a greater metro area, I want to be able to find posts in the small city as well as in the metro area.

In sql, the query would look like WHERE metro-area = 'city123' OR (city = 'city123' AND state = 'state123'). My main problem is that this doesn't seem to be possible with the way WP_QUERY works. My question is - am I overlooking some part of WP_QUERY that may do what I'm after, or is there something else WP provides?

I realize I may come to being forced to build the sql myself, but I'd like to pursue my WP options first.

Your Answer

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

Browse other questions tagged or ask your own question.