• Hi all,

    I’m trying to use the WP_Query object to do quite a complex search – I’d like to search by the post title, custom taxonomies, and custom fields too. The problem is I’d like to get posts which fulfil ANY, not ALL of the requirements… It’s probably easier to explain with code:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    So using this object will return posts of the type ‘restaurant’ which have the ‘$search_query’ in the title or text, but only if they ALSO fit the taxonomy requirements listed under tax_query. I’d like to list restaurants which meet EITHER of these requirements – either the text is in the title, or it has the right taxonomy terms, or both.

    So… to be specific…

    1) How do I list posts which meet either requirment rather than both?

    2) For the first requirement, how do I make it search just the post title, and not the whole content text?

    3) Can I then add a third requirment, involving meta keys and values, also as an ‘OR’ in the eventual query string?

    So essentially I want… in extremely bad pseudo-code/SQL…

    WHERE post_title LIKE $var OR [insert taxonomy query here] OR [insert meta key/value query here] ORDER BY whatever

    Any ideas? Or will I have to write my own custom SQL for this? I’d really rather not if I can possibly help it… :/

    Thanks!

    J.R.

    [No bumping, thank you.]

Viewing 2 replies - 1 through 2 (of 2 total)
  • You can probably do what you want by using filters to modify the query.

    As a general approach to solving problems like this, here is what I do:

    • Use phpMyAdmin or a similar tool to get the query working the way I want it to.
    • Break the query apart into pieces which correspond to the available WP filters, e.g. ‘posts_fields’, ‘posts_join’, etc.
    • Add global variables for each piece to form the correct final query.
    • Add filter functions which check for the global variables and modify the query.

    Here is an article with more detail:

    http://wordpress.mcdspot.com/2010/05/30/filters-to-modify-a-query/

    If it get’s too crazy you might check out a plugin called xili post in post. It makes it much easier to set up complex queries.

    From their site:

    “What happens if the condition is not true ?

    If the condition is false, you can decide to show result of another query.”

    xili-postinpost

    I never used it for anything as complex as what you want, but it very well may make it easier.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Help with WP_Query – searching by many fields’ is closed to new replies.