1 min read
Using wp_dropdown_categories with a custom taxonomy + custom post type
name— theselectname as in<select name="<here>">, and you should set it tothemewhich is your custom taxonomy slug.value_field— the value of the<option>‘s in thatselectdropdown, e.g.<option value="<here>">.And you should set the arg toslugso that when the form is submitted (or when a term is selected), the browser would go tohttps://example.com/?theme=term-slug-hereand then WordPress redirects the browser to the correct term permalink if (and it’s commonly) enabled.selected— the selected option which is a term in the customthemetaxonomy and because the form’s destination is the term archive page, and that a term slug was specified (see the sample URL above), then set this arg toget_query_var( 'theme' )to get the slug of the term being queried on the page.
<form
id="category-select"
class="category-select"
action="<?php bloginfo('url'); ?>" method="get">
<?php
$args = array(
'show_option_all' => __( 'Chews A Breed', 'l-p' ),
'hide_empty' => 0,
'echo' => 0,
'show_count' => 0,
'taxonomy' => 'breeds',
// Add these three args:
'name' => 'breeds', // same as the "taxonomy" above
'value_field' => 'slug', // use the term slug
'selected' => get_query_var( 'breeds' ),
);
$select = wp_dropdown_categories( $args );
$replace = "<select$1 onchange='return this.form.submit()'>";
$select = preg_replace( '#<select([^>]*)>#', $replace, $select );
echo $select;
?>
<noscript><input type="submit" value="View" /></noscript>
</form>
