Couple of notes:
- Your form
action
is incorrect:<form method="get"
. The page is
action="http://www.adoptapet.ie/archive-farm/"><form
method="get" action="http://www.adoptapet.ie/archive-farms/"> -
You should reorder your argument list for
adopt_custom_taxonomy_dropdown()
. As written, most of your
arguments have default values and can be omitted when the function
is called, except that as written argument five doesn’t have a
default so you can’t in practice omit two, three, and four. Move
$name
to the front– much better usability.adopt_custom_taxonomy_dropdown( $taxonomy, $name, $orderby = 'date', $order="DESC", $limit="-1", $show_option_all = null, $show_option_none = null )
- Don’t use
query_posts()
, ever. It clobbers the main query and can
cause numerous problems. - While I have not edited the code into the suggestions below, you
should not ever use user supplied data like$_GET
without
sanitizing it.$_GET
and$_POST
are not safe.
I think your problem is mostly a logic issue. This…
isset($_GET["farm-type"]) && isset($_GET["location-farms"])
…. is always going to be true. $_GET["farm-type"]
and $_GET["location-farms"]
are always going to be set when your form submits, even if they are empty. You should be using !empty()
instead.
And you can clean up this code a lot:
if (!empty($_GET["farm-type"])){
$farm_type = $_GET["farm-type"];
$myquery['tax_query'][] =
array(
'taxonomy' => 'farm-type',
'terms' => array($farm_type),
'field' => 'slug',
);
}
if (!empty($_GET["location-farms"])){
$farm_location = $_GET["location-farms"];
$myquery['tax_query'][] =
array(
'taxonomy' => 'location-farms',
'terms' => array($farm_location),
'field' => 'slug',
);
}
if (!empty($myquery)) {
$q = new WP_Query($myquery); // instead of wp_query()
var_dump($q);
}
And you probably want an AND
relationship between your two components.
if (!empty($myquery)) {
if (1 < count($myquery['tax_query'])) {
$myquery['tax_query']['relation'] = 'AND';
}
$q = new WP_Query($myquery);
var_dump($q->request); // debug
}
Now, if you pass an empty string for your “Select All” option instead of a number, I believe you should get the results you are looking for.