Last active
October 21, 2016 08:22
-
-
Save Mahjouba91/7a815577ddb216dee766363fd8ceb668 to your computer and use it in GitHub Desktop.
Filter admin column in list view with a custom taxonomy
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 | |
add_action( 'restrict_manage_posts' , 'filter_taxonomy_columns' ); | |
/** | |
* Allow admin to filter CPT by custom taxonomies in CPT list view | |
* | |
* @author Florian Tiar | |
*/ | |
function filter_taxonomy_columns( $post_type ) { | |
if ( 'my_cpt' !== $post_type ) { | |
return; | |
} | |
// create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list | |
$filters = array( 'my_taxo1', 'my_taxo2' ); | |
foreach ( $filters as $tax_slug ) { | |
// retrieve the taxonomy object | |
$tax_obj = get_taxonomy( $tax_slug ); | |
$tax_name = $tax_obj->labels->name; | |
$post_formats_args = array( | |
'show_option_all' => 'All ' . $tax_name, | |
'orderby' => 'NAME', | |
'order' => 'ASC', | |
'name' => $tax_slug, | |
'taxonomy' => $tax_slug, | |
'id' => $tax_slug, | |
'value_field' => 'slug', | |
); | |
//if we have a post format already selected, ensure that its value is set to be selected | |
if ( isset( $_GET[ $tax_slug ] ) ) { | |
$post_formats_args['selected'] = sanitize_text_field( $_GET[ $tax_slug ] ); | |
} | |
wp_dropdown_categories( $post_formats_args ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment