Admin Post columns

Custom Post Types columns

When creating custom post type (usually) i find myself having to write the same code over and over for adding or removing admin post listing columns and if you follow my stuff you know that its not my style 🙂
So i coded my self this helper class to simple add or remove admin admin post listing columns for custom post types but as you know it works with posts and pages as well.

The Old Way

if you want to add a custom admin post listing columns to a post type named products , lets say a post thumbnail and a meta field of price you have to:

//first register the column
add_filter('manage_product_posts_columns', 'posts_columns');
function posts_columns($defaults){
	$defaults['price'] = __('Price');
	$defaults['post_thumbs'] = __('Thumbs');
	return $defaults;
}

//then you need to render the column
add_action('manage_product_posts_custom_column', 'posts_custom_columns', 5, 2);
function posts_custom_columns($column_name, $post_id){
	if($column_name === 'price'){
		echo "$" . get_post_meta($post_id,'_price_meta_key', true);
	}
	if($column_name === 'post_thumbs'){
		if (has_post_thumbnail( $post_id ))
			echo the_post_thumbnail( array('80','80') );
		else
			echo "N/A";
	}
}

Not too complicated, so lets also make the price column a sortable column:

// Register the column as sortable
add_filter( 'manage_edit-product_sortable_columns', 'posts_column_register_sortable' );
function posts_column_register_sortable( $columns ) {
	$columns['price'] = 'price';
	return $columns;
}

and last we need to tell WordPress how to handle orderby=price, so:

add_filter( 'pre_get_posts','posts_column_orderby');
function posts_column_orderby( $query ) {
	if( ! is_admin() )  
    	return;

    $orderby = $query->get( 'orderby');
    if ( 'price' == $orderby ) {
    	//alter the query args
   		$query->set('meta_key',$orderby);
   		$query->set('orderby','meta_value_num');
    }
}//end _column_orderby

Still Nothing too complicated but just the fact of doing it over each time and for each column ...

The easy way

So using this class to achieve the same would look like this:

//create an instance
$product_columns = new CPT_columns('product');
//add thumb column
$post_columns->add_column('post_thumb',
	array(
		'label' => __('Thumb'),
		'type'  => 'thumb'
	)
);
//add sortable price column
$post_columns->add_column('price',
	array(
		'label'    => __('price'),
		'type'     => 'post_meta',
		'meta_key' => 'price', //meta_key
		'orderby' => 'meta_value_num',
		'sortable' => true,
		'prefix' => "$",
		'suffix' => ""
	)
);

Now this is a much nicer and clean solution.

The Code

Or just be a douche and go to https://gist.github.com/bainternet/80a8406c9e714ad6ad37

As always your questions, suggestions and feedback is welcome.

Ohad Raz

WordPress Consultant, a WordPress Developer and a WordPress Freelancer With over 10 years experience in architecting web sites and applications. WordPress Development moderator and somethimes Plugin Developer.