Problem: Currently if I leave the selects empty it sends them as empty strings in the get url (?s=keyword&country=&year=). This means that nothing will be found in the search. (I am guessing because its saying 'find matching posts with 'keyword' AND an empty string). So I want ?s=keyword to be passed if others are unused.

Perhaps I've setup the code wrong, likely as I've just started playing around with WP_Query.

1 Answer
1

That's certainly something you must solve on the server side, only omitting the arguments in URL would cause errors when accessing the non-existent keys in the $_GET array.

You should build your arguments array dynamically based on whether the values are set ot not.

Example:

$args = array();
foreach(array('s', 'country', 'year') as $key)
{
// if key is available and not empty, include argument in the query
if(isset($_GET[$key]) && trim($_GET[$key]) !== '')
{
$args[$key] = $_GET[$key];
}
}
// 's' is neccessary for a search query, so only continue if it's available
if(isset($args['s']))
{
$the_query = new WP_Query($args);
...
}

Update:

After figuring out what this country plugin is, I had a look at it. It manipulates the query, but it only checks whether the country key is present, not if it's empty, and it does this check on the global query object, so even if there is no country in your custom query, it will be present in the global query object because the plugin added the country key to the list of variables that will be fetched automatically (in case you didn't knew, WordPress will automatically query the posts for you). You may want to contact the plugin author and tell him about the problem so that he fixes it, until then, here's a quick and dirty fix for the current version:

Thanks for getting back to me, I had been trying out all the if else statements but I retracted since it was so messy. Your solution is much cleaner. I have a print_r($args) and it previously showed empty array items. Your conditional has removed these from the print_r showing only s => keyword but still no results show. NOTE: if I remove the empty parameters from URL the page displays results. Any further thoughts?
–
GraemeOct 11 '12 at 11:03

Well, the problem could be anywhere, I've just did a quick test and it works fine for me. Have a look at $the_query->request to make sure the generated query is valid and that it will match anything at all.
–
ndmOct 11 '12 at 13:02

Odd. It wasn't working before. Thanks, that has certainly got me well on the right track!
–
GraemeOct 11 '12 at 13:16

Hi, still having problems with this again. When searching without select a country and only a keyword I get no results ?s=a&country=. But if i remove the country manually from the url it pulls results ?s=a. Why is this? It's really stumped me. I need to allow for people who do not want to refine their search.
–
GraemeOct 15 '12 at 16:09

It works fine for me with all possible key combinations, however i don't know your country plugin, there's a good chance that it manipulates the query and doesn't do so correctly, so as I recommended before, please have a look at $the_query->request to check if and what exactly is wrong with the generated query. Also check if the plugin is actually hooking in the query process anywhere.
–
ndmOct 15 '12 at 16:56