1 min read

Using wp_dropdown_categories with a custom taxonomy + custom post type

  • name — the select name as in <select name="<here>">, and you should set it to theme which is your custom taxonomy slug.
  • value_field — the value of the <option>‘s in that select dropdown, e.g. <option value="<here>">.And you should set the arg to slug so that when the form is submitted (or when a term is selected), the browser would go to https://example.com/?theme=term-slug-here and 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 custom theme taxonomy 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 to get_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>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.