Created
April 12, 2024 21:15
-
-
Save taricco/cb85a7a1e93d350a6284e7940687a8f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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