• Take each word in a sentence and do the conditional search with OR and bring results that contain any of those words in the post. I’m using the wp query s parameter.

Viewing 3 replies - 1 through 3 (of 3 total)
  • What is the question? This feature is part of core.

    Moderator bcworkz

    (@bcworkz)

    You want OR logic applied to each word instead of the default AND logic, right? You could use the “posts_request” filter to alter the actual SQL used. Seek out the appropriate AND and change it to OR. Easier said than done though. It won’t do to change all occurrences of AND. The challenge is in changing the right ones.

    I’d be inclined to compose my own WHERE clause in it’s entirety through the “posts_where” filter. Callbacks are passed the entire WP_Query object, from which the “s” query var can be used to compose your own WHERE clause.

    There are various advanced search plugins. I’d expect at least a few would apply the OR logic you want without needing to custom code a solution.

    When I read the code here: https://developer.wordpress.org/reference/classes/wp_query/parse_search/
    it looks like the default is OR.
    And when I use the search I usually want it to be AND.

    Edit: It is OR between content, title, excerpt.
    But if I put more words, I don’t want more matches, I want fewer matches (use AND between words).

    • This reply was modified 5 years, 2 months ago by Joy. Reason: clarify
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Search with Wp Query’ is closed to new replies.