15
votes

I have created a custom post type named banners. Thereby I register a new taxonomy called location that specifies on which page the banner is to be shown. Everything is great however when I click on the custom posts type 'Banner' in the admin window I see all the banners created however the table does not have a column for the taxonomy 'Location'.

In other words I want to be able to see what location the banner is in, in the banners listing.

4

4 Answers

35
votes

For those interested, the register_taxonomy function, as of WordPress 3.5, now offers an argument for show_admin_column (false by default). Set to true, it automatically displays the taxonomy column in the admin.

9
votes

You can use the manage_post-type_custom_column and manage_edit_post-type_columns filters to add your taxonomy column to the post type listing.

add_action( 'admin_init', 'my_admin_init' );
function my_admin_init() {
    add_filter( 'manage_edit-banner_columns', 'my_new_custom_post_column');
    add_action( 'manage_banner_custom_column', 'location_tax_column_info', 10, 2);
}

function my_new_custom_post_column( $column ) {
    $column['location'] = 'Location';

    return $column;
}

function location_tax_column_info( $column_name, $post_id ) {
        $taxonomy = $column_name;
        $post_type = get_post_type($post_id);
        $terms = get_the_terms($post_id, $taxonomy);

        if (!empty($terms) ) {
            foreach ( $terms as $term )
            $post_terms[] ="<a href='edit.php?post_type={$post_type}&{$taxonomy}={$term->slug}'> " .esc_html(sanitize_term_field('name', $term->name, $term->term_id, $taxonomy, 'edit')) . "</a>";
            echo join('', $post_terms );
        }
         else echo '<i>No Location Set. </i>';
}
7
votes
 //what version of wordpress you are using
 //since wp 3.5 you can pass parameter show_admin_column=>true
 // hook into the init action and call create_book_taxonomies when it fires
 add_action( 'init', 'create_book_taxonomies', 0 );

 // create two taxonomies, genres and writers for the post type "book"
 function create_book_taxonomies() {
 // Add new taxonomy, make it hierarchical (like categories)
$labels = array(
    'name'              => _x( 'Genres', 'taxonomy general name' ),
    'singular_name'     => _x( 'Genre', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Genres' ),
    'all_items'         => __( 'All Genres' ),
    'parent_item'       => __( 'Parent Genre' ),
    'parent_item_colon' => __( 'Parent Genre:' ),
    'edit_item'         => __( 'Edit Genre' ),
    'update_item'       => __( 'Update Genre' ),
    'add_new_item'      => __( 'Add New Genre' ),
    'new_item_name'     => __( 'New Genre Name' ),
    'menu_name'         => __( 'Genre' ),
);

$args = array(
    'hierarchical'      => true,
    'labels'            => $labels,
    'show_ui'           => true,
    'show_admin_column' => true,
    'query_var'         => true,
    'rewrite'           => array( 'slug' => 'genre' ),
);

register_taxonomy( 'genre', array( 'book' ), $args );
 }
0
votes

In addition to Jonathan Wold answer, you can add the ability to filter by your custom taxonomy like this:

add_action('restrict_manage_posts', 'filter_post_type_backend_by_taxonomies', 99, 2);

function filter_post_type_backend_by_taxonomies($post_type, $which) {
    if( 'your_post_type' !== $post_type) {
        return;
    }
    
    $taxonomies = array( 'your_custom_taxonomy' );
    
    foreach( $taxonomies as $taxonomy_slug ) {
        $taxonomy_obj = get_taxonomy( $taxonomy_slug );
        $taxonomy_name = $taxonomy_obj->labels->name;
        
        $terms = get_terms($taxonomy_slug);
        
        echo "<select name='{$taxonomy_slug}' id='{$taxonomy_slug}' class='postform'>";
        echo '<option value="">' . sprintf( esc_html__( 'Show all %s', 'domain-name' ), $taxonomy_name) . '</option>';
        foreach ( $terms as $term ) {
            printf(
                    '<option value="%1$s" %2$s>%3$s (%4$s)</option>',
                    $term->slug,
                    ( ( isset( $_GET[$taxonomy_slug] ) && ( $_GET[$taxonomy_slug] == $term->slug ) ) ? ' selected="selected"' : '' ),
                    $term->name,
                    $term->count
            );
            }
        echo '</select>';
    }
}