Skip to content

Instantly share code, notes, and snippets.

@ScottPhillips
Created July 29, 2012 15:12
Show Gist options
  • Save ScottPhillips/3199420 to your computer and use it in GitHub Desktop.
Save ScottPhillips/3199420 to your computer and use it in GitHub Desktop.
Add Custom Columns to WordPress Post List Page
<?php
add_filter( 'manage_posts_columns', 'your_custom_cols' ); //Filter out Post Columns with 2 custom columns
function your_custom_cols($defaults) {
$defaults['language'] = __('Language'); //Language and Films is name of column
$defaults['film'] = __('Films');
return $defaults;
}
add_action('manage_posts_custom_column', 'custom_cols_data', 10, 2); //Just need a single function to add multiple columns
function custom_cols_data($column_name, $post_id) {
global $wpdb;
if( $column_name == 'language' ) {
$tags = get_the_terms($post->ID, 'lang'); //lang is the first custom taxonomy slug
if ( !empty( $tags ) ) {
$out = array();
foreach ( $tags as $c )
$out[] = &quot;<a href='edit.php?lang=$c->slug'> &quot; . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'lang', 'display')) . &quot;</a>&quot;;
echo join( ', ', $out );
} else {
_e('No Languages.'); //No Taxonomy term defined
}
} else if( $column_name == 'film' ) { //Adding 2nd column
$tags = get_the_terms($post->ID, 'film'); //film is the next taxonomy slug for column Films
if ( !empty( $tags ) ) {
$out = array();
foreach ( $tags as $c )
$out[] = &quot;<a href='edit.php?film=$c->slug'> &quot; . esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'film', 'display')) . &quot;</a>&quot;;
echo join( ', ', $out );
} else {
_e('No Related Films.'); //no Films found
}
} else {
echo '<i>'.__('None').'</i>'; //Only 2 columns, blank now.
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment