Add Taxonomy to Edit Post Columns

<?php
/**
* Taxonomy Post Columns
* @link http://www.billerickson.net/code/add-taxonomy-to-edit-post-columns
*
* @package Core_Functionality
* @since 1.0.0
* @link https://github.com/billerickson/Core-Functionality
* @author Bill Erickson <[email protected]>
* @copyright Copyright (c) 2011, Bill Erickson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*/
/**
* Custom Post Columns
* @link http://scribu.net/wordpress/custom-sortable-columns.html
*
* @param array $defaults
* @return array
*/
function be_custom_post_columns($defaults) {
$defaults['technology'] = 'Technologies';
$defaults['industry'] = 'Industries';
$defaults['product'] = 'Products';
unset( $defaults['tags'] );
return $defaults;
}
add_filter( 'manage_edit-post_columns', 'be_custom_post_columns' );
/**
* Custom Post Columns Data
*
* @param string $column_name
* @param int $post_id
* @return null
*/
function be_custom_post_columns_data( $column_name, $post_id ) {
$taxonomies = array( 'technology', 'industry', 'product' );
$post_type = 'post';
foreach( $taxonomies as $taxonomy ) {
if( $column_name == $taxonomy ) {
$terms = get_the_terms( $post_id, $taxonomy );
if ( !empty( $terms ) ) {
$output = array();
foreach ( $terms as $term )
$output[] = '<a href="' . admin_url( 'edit.php?' . $taxonomy . '='. $term->slug . '&post_type=' . $post_type ) . '">' . $term->name . '</a>';
echo join( ', ', $output );
}
else {
_e('Uncategorized');
}
}
}
}
add_action( 'manage_posts_custom_column', 'be_custom_post_columns_data', 10, 2 );
/**
* Make Custom Columns Sortable
*
* @param array $columns
* @return array
*/
function be_custom_post_columns_sortable( $columns ) {
$columns['technology'] = 'technology';
$columns['industry'] = 'industry';
$columns['product'] = 'product';
return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'be_custom_post_columns_sortable' );
/**
* Custom Post Columns Orderby
*
* @param array $args
* @return array
*/
function be_custom_post_columns_orderby( $args ) {
$taxonomies = array( 'technology', 'industry', 'product' );
if ( isset( $args['orderby'] ) && in_array( $args['orderby'], $taxonomies ) ) {
$taxonomy = $args['orderby'];
$tax_terms = get_terms( $taxonomy, array( 'fields' => 'ids' ) );
$args = array_merge( $args, array(
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'id',
'terms' => $tax_terms,
)
)
) );
}
return $args;
}
add_filter( 'request', 'be_custom_post_columns_orderby' );

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk