I am trying to create a Search Filter within WordPress to filter through all the custom taxonomies within a Custom Post Type.
I have created a function that generates the dropdown so I can output each option value as a slug for my filter which is working already. My only issue is that the "Select All" option is not working.
The results come back with nothing found when Selecting All. The filter can be here.
The code I have used to create the function is:
function adopt_custom_taxonomy_dropdown( $taxonomy, $orderby = 'date', $order = 'DESC', $limit = '-1', $name, $show_option_all = null, $show_option_none = null ) {
$args = array(
'orderby' => $orderby,
'order' => $order,
'number' => $limit,
);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
printf( '<select name="%s" class="postform">', esc_attr( $name ) );
if ( $show_option_all ) {
printf( '<option value="0">%s</option>', esc_html( $show_option_all ) );
}
if ( $show_option_none ) {
printf( '<option value="-1">%s</option>', esc_html( $show_option_none ) );
}
foreach ( $terms as $term ) {
printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
}
print( '</select>' );
}
}
and this is where the results are being pulled to...
<?php
if (isset($_GET["farm-type"]) && empty($_GET["location-farms"])){
$farm_type = $_GET["farm-type"];
$myquery1['tax_query'] = array(
array(
'taxonomy' => 'farm-type',
'terms' => array($farm_type),
'field' => 'slug',
),
);
query_posts($myquery1);
}
?>
<?php
if (isset($_GET["farm-type"]) && isset($_GET["location-farms"])){
$farm_type = $_GET["farm-type"];
$farm_location = $_GET["location-farms"];
$myquery2['tax_query'] = array(
array(
'taxonomy' => 'farm-type',
'terms' => array($farm_type),
'field' => 'slug',
),
array(
'taxonomy' => 'location-farms',
'terms' => array($farm_location),
'field' => 'slug',
),
);
query_posts($myquery2);
}
?>
query_posts
, there is no reason this couldn't use aWP_Query
object or thepre_get_posts
filter to implement this