Multiple Search Terms WP_Query


I’ve tried searching to no avail, and it may be that I’m not sure what it is I’m actually searching for. I am trying to use the WP_Query to display a list of posts that contain ANY (not ALL) of the keywords.

So, here is my code:

  $queertionary_index_all = new WP_Query(
      'post_type' => 'Definitions',
      'posts_per_page' => 30,
      'order' => 'ASC',
      'post__not_in' => array(get_the_ID()),
      'orderby' => 'relevance',
      'sentence' => false,
       's' => 'fairy',

This, rightly, displays all the posts that contain the term "fairy".

However, I want it to display all the posts that contain EITHER the term "fairy" OR the term "Ursula".

I have tried numerous things, such as ‘s’ => array(‘fairy’,’Ursula’), but this either displays ALL the posts, regardless of whether they contain "fairy" or "Ursula" or not, or it displays none at all (I’m assuming because none of the posts contain both terms).

Is there a way to do this?

I’m curious as to whether the solution is in functions.php, because I notice that using the search form for "fairy" does as it’s supposed to, but searching for "fairy Ursula" will bring up nothing, because as said, there’s no posts that contain BOTH terms.

It’d be great if there was a solution that could universally solve this, so that "fairy Ursula" would work in both search results and WP_Query to bring up posts with EITHER "fairy" or "Ursula" in them, but if not, my priority would be a WP_Query solution.

I’d really prefer not to use a plugin if possible.

Any help you could offer me in this respect would be very gratefully appreciated!

Many thanks.

T. K. Bird 1 month 0 Answers 17 views 0

Leave an answer