Basically what I am trying to do is to have a number of input fields on my WordPress search form as follows:

All of these words
The exact phrase
Any of these words
None of these words

and then to put those together into one array to pass through to the results page. (excuse the terminology - hope that makes sense!)

What I am thinking is that, for example, if someone wanted to search posts with the word "lecture" and "2016" but not "2015" or "2014".

For example, if I input the below into the standard WordPress search field:

"lecture +2016 - 2014"

I get this in the querystring:


What I am needing is to create that manually from the fields above. I would imagine that I would need to do something like put a plus sign in front of each word in the "All of these words" field and a minus sign in front of the words in the "None of these words" field?

But how do I access and change that query?

Any advice/direction would be greatly appreciated!

Thanks in advance!

This is a request from the site users so that is why I am trying to make the form a bit more intuitive rather than having users needing to use operators such as the negative sign in "-2014"

As far as I can tell what would help is if I could somehow amend/alter the "s" parameter in the search before it is actually "processed"

So if I had the following:

All of these words: "lecture 2016"
None of these words: "2014"

I could somehow extract the "s" value and then regenerate that parameter into something like:

$args = array (
's' => '\"lecture 2016\" -2014',

that should work - is this on the right track?

Edit: got the s parameter to change using:

function search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
        $query->set( 's', 'stackoverflow' );


1 Answer 1


Use pre_get_posts to modify the query object before the query has run. - https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-1347' );
    else if ($query->is_search) {
        $query->set( 's', 'stackoverflow' );
add_action( 'pre_get_posts', 'exclude_category' );
  • Thanks, I see that for modifying the query object but cannot seem to get it to work to change the "s" parameter. Aug 8, 2016 at 3:21
  • Thanks, I now have manged to change the s parameter - not exactly where/how I wanted on my side (!) but you have answered the question. See edit of my question. Aug 8, 2016 at 3:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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