Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taricco/cb85a7a1e93d350a6284e7940687a8f5 to your computer and use it in GitHub Desktop.
Save taricco/cb85a7a1e93d350a6284e7940687a8f5 to your computer and use it in GitHub Desktop.
// Add taxonomy filter dropdowns to the Resources CPT admin page
function wsv_resources_category_filter() {
global $typenow;
if ($typenow == 'resources') {
$taxonomies = array('resource-type');
if (!empty($taxonomies)) {
foreach ($taxonomies as $taxonomy) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => sprintf(__('Show all %s', 'textdomain'), $info_taxonomy->label),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
}
}
}
}
add_action('restrict_manage_posts', 'wsv_resources_category_filter');
// Filter Resources CPT by selected taxonomy
function wsv_resources_filter_query($query) {
global $pagenow;
if ($pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'resources') {
$taxonomies = array('resource-type');
foreach ($taxonomies as $taxonomy) {
if (isset($_GET[$taxonomy]) && $_GET[$taxonomy] != '') {
$term = get_term_by('slug', $_GET[$taxonomy], $taxonomy);
if ($term) {
$query->query_vars[$taxonomy] = $term->slug;
}
}
}
}
}
add_filter('parse_query', 'wsv_resources_filter_query');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment