Skip to content

Instantly share code, notes, and snippets.

@Mahjouba91
Last active October 21, 2016 08:22
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 Mahjouba91/7a815577ddb216dee766363fd8ceb668 to your computer and use it in GitHub Desktop.
Save Mahjouba91/7a815577ddb216dee766363fd8ceb668 to your computer and use it in GitHub Desktop.
Filter admin column in list view with a custom taxonomy
<?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