Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Custom search form with empty parameters

Hopefully this is a quick one, but I've hit a wall here.

I have multiple search terms, keyword, category, country and date. I've set it up so that the user doesn't have to search all of these filters. I can leave off category and/or date and it will show looser results.

But if I leave off country it shows no results, doing the same to any other parameter still shows results. The problem lies with country only.

/?s=a&cat=41&country=&monthnum=&year= produces no results but

/?s=a&cat=41&monthnum=&year= does.

Why is this? Surely these are the same thing?

This is my wp_query, simply pulls posts, I had the foreach to clear this issue before but currently if I remove it. The search works no differently.

$args = array('post_type' => 'post')
// This foreach doesn't actually need to be here ?
foreach(array('s', 'cat', 'country', 'monthnum', 'year') as $key) {
    if(isset($_GET[$key]) && trim($_GET[$key]) !== '') {
        $args[$key] = $_GET[$key];
    }
}
share|improve this question
    
My best guess is that country is a reserved parameter in wordpress. Changing "country" to something like "abc_country" might get it to work. –  Mridul Aggarwal Oct 15 '12 at 19:56
    
A fair guess but it is not –  Graeme Oct 16 '12 at 8:54
    
Answer found on a separate <A href="wordpress.stackexchange.com/questions/67790/…;: –  Graeme Oct 17 '12 at 11:23
add comment

marked as duplicate by toscho Oct 17 '12 at 11:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

Your query variable country is not defined, its not part of the standard query_vars set by WordPress, where as both monthnum and year are and that's why they work and the former does not.

So you need to add your query variable into the mix, try the following;

function new_query_var() {

    global $wp;
    $wp->add_query_var('country');

}

add_filter('init', 'new_query_var');

Also, it appears that your foreach is checking for the existing of this query variable and hence since its not set (e.g. isset) then it fails the conditional check.

share|improve this answer
    
Hi thanks for the answer. And yes, that makes sense about the foreach. I added your function into functions.php but still no change. It refused to find anything if the country= is present. Or have I misunderstood you? –  Graeme Oct 15 '12 at 13:11
    
Replace the if { } block temporarily with var_dump($key); what do you see? –  userabuser Oct 15 '12 at 13:15
    
I got this string(1) "s" string(3) "cat" string(7) "country" string(8) "monthnum" string(4) "year" from these url parameters /?s=a&cat=41&country=&monthnum=10&year=2012 –  Graeme Oct 15 '12 at 13:18
add comment

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