Last active
May 22, 2020 15:46
-
-
Save steve-brett/59a6f8d4289233239e1edce1fb3c3a7c to your computer and use it in GitHub Desktop.
Filter custom post type by tag
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
<?php | |
/** | |
* Adds admin area filtering for custom post type tags. | |
* | |
* This is separate because, for some reason, the taxonomy | |
* is 'post_tag' but the query var is 'tag'. | |
* | |
* @param string $post_type The post type slug. | |
* @return void | |
*/ | |
function filter_cpt_by_tag( $post_type ) { | |
// Apply this only on a specific post type | |
if ( 'example_post_type' !== $post_type ) { | |
return; | |
} | |
$params = array( | |
'show_option_all' => get_taxonomy('post_tag')->labels->all_items, | |
'hide_empty' => true, | |
'hierarchical' => 1, | |
'show_count' => true, | |
'orderby' => 'name', | |
'name' => 'tag', | |
'value_field' => 'slug', | |
'taxonomy' => 'post_tag', | |
); | |
if ( isset($_GET['tag']) ) { | |
$params['selected'] = $_GET['tag']; | |
} | |
wp_dropdown_categories($params); | |
} | |
add_action( 'restrict_manage_posts', 'filter_cpt_by_tag' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment