Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created May 23, 2012 18:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save billerickson/2776910 to your computer and use it in GitHub Desktop.
Save billerickson/2776910 to your computer and use it in GitHub Desktop.
<?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 <bill@billerickson.net>
* @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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment