Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save foxinni/2922611 to your computer and use it in GitHub Desktop.
Save foxinni/2922611 to your computer and use it in GitHub Desktop.
Modify wp_dropdown_categories() to support taxonomy->slug in select output, and not taxonomy->id.
<?php
/*
Background: My company post type needed a filter added via the 'restrict_manage_posts' action, and I needed to modify the wp_dropdown_categories to output the taxonomy slug into the <option value="..."> instead of the taxonomy ID. The taxonomy being 'industry'.
The SH_Walker_TaxonomyDropdown() can be found here: https://gist.github.com/2902509
*/
function restrict_listings_by_taxonomy() {
global $typenow;
global $wp_query;
if ($typenow == 'company' ) {
$taxonomy = 'industry';
$current_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(
array(
'walker'=> new SH_Walker_TaxonomyDropdown(),
'value'=>'slug',
'show_option_all' => __("Show All {$current_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => 'industry',
'orderby' => 'name',
'selected' => $wp_query->query['term'],
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // Show # companies in parents
'hide_empty' => true, // Don't show companies w/o listings
)
);
}
}
@whyisjake
Copy link

This is great, thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment