[Snippet] Adding your custom taxonomy to post/page list in WordPress

Custom Taxonomies are amazing for organising your post data. Here’s a simple way of showing your custom taxonomies on the post/page list in wp-admin.

In the image below you can see we are now showing the custom taxo Brand in the post list.

 

Here’s how I did it

Firstly, create your extra column. You can have as many or as little as you want

function add_extra_column($posts_columns) {
	$posts_columns['ctag'] = _x('Brand', 'column name');
	return $posts_columns;
}

Don’t forget you can unset() columns you want to hide aswell

unset($post_columns['col_name'])

Secondly, this is our function to populate that new column with data.
Change brand to your custom taxonomy.

function manage_attachment_tag_column($column_name, $id) {
	switch($column_name) {
		case 'ctag':
			$tags = get_the_terms($id,'brand');
			if (!empty($tags)) {
				$out = array();
				foreach ( $tags as $c )
					$out[] = esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'post_tag', 'display'));
				echo join( ', ', $out );
			} else {
				_e('No Tags');
			}
			break;
		default:
			break;
	}
}

Finally, add the filter and hook.

add_filter('manage_edit-post_columns', 'add_extra_column');
add_action('manage_posts_custom_column', 'manage_attachment_tag_column', 10, 2);

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *