Skip to content

Commit

Permalink
Merge pull request #81 from jjgrainger/fix/taxonomy-filter-dropdown
Browse files Browse the repository at this point in the history
Fix Taxonomy dropdown filter
  • Loading branch information
jjgrainger authored May 21, 2022
2 parents e25a4d9 + 1f618cd commit 28c5841
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/PostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,21 +411,14 @@ public function modifyFilters($posttype)
continue;
}

// get the taxonomy object
$tax = get_taxonomy($taxonomy);

// get the terms for the taxonomy
$terms = get_terms([
'taxonomy' => $taxonomy,
'orderby' => 'name',
'hide_empty' => false,
]);

// if there are no terms in the taxonomy, ignore it
if (empty($terms)) {
// If the taxonomy is not registered to the post type, continue.
if (!is_object_in_taxonomy($this->name, $taxonomy)) {
continue;
}

// get the taxonomy object
$tax = get_taxonomy($taxonomy);

// start the html for the filter dropdown
$selected = null;

Expand All @@ -434,19 +427,21 @@ public function modifyFilters($posttype)
}

$dropdown_args = [
'option_none_value' => '',
'hide_empty' => 0,
'hide_if_empty' => false,
'show_count' => true,
'taxonomy' => $tax->name,
'name' => $taxonomy,
'orderby' => 'name',
'hierarchical' => true,
'show_option_none' => "Show all {$tax->label}",
'value_field' => 'slug',
'selected' => $selected
'name' => $taxonomy,
'value_field' => 'slug',
'taxonomy' => $tax->name,
'show_option_all' => $tax->labels->all_items,
'hierarchical' => $tax->hierarchical,
'selected' => $selected,
'orderby' => 'name',
'hide_empty' => 0,
'show_count' => 0,
];

// Output screen reader label.
echo '<label class="screen-reader-text" for="cat">' . $tax->labels->filter_by_item . '</label>';

// Output dropdown for taxonomy.
wp_dropdown_categories($dropdown_args);
}
}
Expand Down

0 comments on commit 28c5841

Please sign in to comment.