Shiba

Adventures in WordPress

  • Home
  • Dog
  • Art
  • Contact
  • WordPress Articles
    • WP Plugins
    • WP Programming
    • WP Admin Panels
    • WP Theme Design
    • WP How-To
    • WP Theme Images
You are here: Home / WordPress Admin / Custom Post Type / Add Custom Post Type Columns

Add Custom Post Type Columns

by ShibaShake 88 Comments

One of the exciting new features in WordPress 3.0 is custom post types. We can create our own post types by using the register_post_type function.

If we enable UIs for our custom post type, we will get additional menu items on our WordPress dashboard similar to the Edit and Add New options for standard posts.

Here, we consider how to add new columns to the Edit custom post type screen.

1. Create a Custom Post Type

Creating a custom post type is surprisingly straight-forward and well documented in the WordPress Codex.

In this example, we create a custom post type called gallery.

	$labels = array(
		'name' => _x('Galleries', 'post type general name'),
		'singular_name' => _x('Gallery', 'post type singular name'),
		'add_new' => _x('Add New', 'gallery'),
		'add_new_item' => __("Add New Gallery"),
		'edit_item' => __("Edit Gallery"),
		'new_item' => __("New Gallery"),
		'view_item' => __("View Gallery"),
		'search_items' => __("Search Gallery"),
		'not_found' =>  __('No galleries found'),
		'not_found_in_trash' => __('No galleries found in Trash'), 
		'parent_item_colon' => ''	  );
	  $args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true, 
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array('title','thumbnail','excerpt')
	  ); 
	  register_post_type('gallery',$args);

New gallery custom post type menu items and Edit screen.
New gallery custom post type menu items and Edit screen.

2. Add New Custom Post Type Columns

As shown in the screen-shot above, our custom post type Edit screen (called Gallery) starts off with four columns – checkbox, Title, Author, and Date.

To add new columns to our Edit screen, we want to hook into the manage_$pagename_columns filter. The $pagename of the Edit screen is edit-$post_type.

Therefore, in this example, –

  • Edit page name = edit-gallery
  • Add column filter hook = manage_edit-gallery_columns

Our add column function call looks like this –

// Add to admin_init function
add_filter('manage_edit-gallery_columns', 'add_new_gallery_columns');

Our filter function accepts an array of column names, and returns our new column array once we are done.

	function add_new_gallery_columns($gallery_columns) {
		$new_columns['cb'] = '<input type="checkbox" />';
		
		$new_columns['id'] = __('ID');
		$new_columns['title'] = _x('Gallery Name', 'column name');
		$new_columns['images'] = __('Images');
		$new_columns['author'] = __('Author');
		
		$new_columns['categories'] = __('Categories');
		$new_columns['tags'] = __('Tags');
	
		$new_columns['date'] = _x('Date', 'column name');
	
		return $new_columns;
	}

In the example above we fully replace the column array with our own entries. We can also just add columns by adding new elements into the existing $gallery_columns array. However, our added columns will only appear after the existing default columns.

The functions above will add new columns into our Edit Gallery screen which now looks like this –

Gallery screen with new custom post type columns.
Gallery screen with new custom post type columns.

If we want the column to be sortable, then we can use the manage_{$screen->id}_sortable_columns filter. Here is a good example for making sortable columns.

3. Render Our New Custom Post Type Columns

Note – In the screen-shot above, the ID and Images columns are empty because they are not standard WordPress post columns. Standard WordPress post columns include –

  • ‘cb’ – Post checkbox.
  • ‘date’ – Date when post was last modified.
  • ‘title’ – Post title and common post actions including Edit, Quick Edit, Trash, and View.
  • ‘categories’ – Post categories.
  • ‘tags’ – Post tags.
  • ‘comments’ – Number of post comments.
  • ‘author’ – Post author.

To render our new columns, ‘id’ and ‘images’, we must hook into the manage_posts_custom_column action (in WordPress 3.0).

In WordPress 3.1, we want to hook into the manage_{$post_type}_posts_custom_column action. Since our example post type is gallery, we want to hook into manage_gallery_posts_custom_column.

        // Add to admin_init function
	add_action('manage_gallery_posts_custom_column', 'manage_gallery_columns', 10, 2);

	function manage_gallery_columns($column_name, $id) {
		global $wpdb;
		switch ($column_name) {
		case 'id':
			echo $id;
		        break;

		case 'images':
			// Get number of images in gallery
			$num_images = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = %d;", $id));
			echo $num_images; 
			break;
		default:
			break;
		} // end switch
	}	

Our ‘id’ and ‘images’ columns will now be rendered with the proper values.

Render new custom post type columns with the proper data.
Render new custom post type columns with the proper data.

We Are Done!

Another interesting action hook in the Edit screen page is restrict_manage_posts which allows us to control which custom post objects we want to show on the page.

And just like that … we are done!

Leave a Reply to ShibaShake Cancel reply

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

Comments

« Previous 1 2 3 4 5 Next »
  1. Slob Jones says

    March 20, 2012 at 7:58 pm

    What about adding a column for custom taxonomies, as well as for categories?

    Reply
  2. Sigondrong dari Gua Hiro says

    January 25, 2012 at 4:07 am

    If we make a custom post type, so we also may have a different field to be edited. How to insert custom field (may be, not as postmeta but taken from different table) in edit post page?
    ie in: /wp-admin/post.php?post=xx&action=edit

    Reply
    • ShibaShake says

      January 25, 2012 at 10:55 am

      http://shibashake.com/wordpress-theme/add-metabox-custom-post-type

      Reply
    • Sigondrong dari Gua Hiro says

      January 25, 2012 at 4:08 pm

      I found this link http://codex.wordpress.org/Function_Reference/add_meta_box
      This is what I want.

      Reply
    • Sigondrong dari Gua Hiro says

      January 25, 2012 at 4:13 pm

      But http://shibashake.com/wordpress-theme/add-metabox-custom-post-type is very good. Thanks a lot.

      Reply
  3. Erik F says

    December 13, 2011 at 12:13 am

    Wonderful, this solves many of my problems. However, I have a question, how do I show the galleries for the visitor? Is it through single.php?

    Reply
    • Erik F says

      December 14, 2011 at 4:05 am

      When I try to look at my new gallery, I see only what’s in 404.php. What am I doing wrong? Is there any way I can list all the galleries on one page (such as posts)?

      Reply
      • ShibaShake says

        January 4, 2012 at 3:07 pm

        When you say new gallery, are you using the WordPress native gallery or the gallery object from Shiba Media Library? When you edit the gallery in the dashboard, do all the images show up? What do you have your gallery permalink set to?

  4. Hatem says

    November 26, 2011 at 11:28 pm

    Thanx , your post is so great. But i have a Q related to categories columns. It is all time (uncategorized) even if the custom type post categorized !!

    How i can solve this ?!

    Reply
    • iain says

      February 9, 2012 at 1:51 pm

      Hi did you ever find an answer to this?

      Reply
      • iain says

        February 9, 2012 at 1:57 pm

        http://www.ilovecolors.com.ar/add-taxonomy-columns-custom-post-types-wordpress/

        Just seen this links!!!! works well!!! hope it helps

  5. Abzal says

    September 28, 2011 at 7:56 am

    Hey, nice article! I am planning to create custom post types for Restaurants with menus and others things. I am unsure of having one CPT or many; say one for Restaurant, menus and later Location.

    Thank you!

    Reply
  6. Greg says

    September 23, 2011 at 9:40 am

    Great post — it’s unbelievable the amount of Googling I had to do to find a satisfactory (and clear!) method to do this. Thanks for letting me end my search!

    Reply
    • ShibaShake says

      September 23, 2011 at 11:16 am

      Glad to be of service. 🙂

      Reply
  7. wp_harish says

    September 22, 2011 at 4:40 am

    Hey thanks very much..
    I am eager to know how Can we add functionality to custom column for sorting of posts…just like Title and Date columns come by default..

    Reply
    • ShibaShake says

      September 23, 2011 at 11:08 am

      Use the “manage_{$screen->id}_sortable_columns” filter.

      Reply
  8. prajesh says

    September 15, 2011 at 4:01 am

    Very useful article

    Reply
  9. George Stephanis says

    September 7, 2011 at 7:54 am

    Here’s what I did to insert two columns in the middle of a custom post type table … which is also more extensible, as it lets other plugins do the same:

    
    	function add_new_columns( $cols ){
    		return array_merge(
    			array_slice( $cols, 0, 2 ),
    			array( 	'subtitle' => __('Subtitle'),
    					'pub_date' => __('Publication Date') ),
    			array_slice( $cols, 2 )
    		);
    	}
    

    If you wanted to be even pickier, instead of hardcoding `2` for after the second column, you could use something to find the index of the associative key, either directly or by snatching an array of the keys and searching in there with array_keys();

    Reply
  10. toy says

    July 19, 2011 at 9:02 am

    this was awesome!
    thanks so much

    Reply
« Previous 1 2 3 4 5 Next »

Recent Posts

  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text
  • Screenshot of the Success, WordPress has been installed page.Migrating Your WordPress Website to Amazon EC2 (AWS)
  • Screenshot of WinSCP for creating a SFTP configuration.How to Set-Up SFTP on Amazon EC2 (AWS)
  • WordPress Gutenberg code view screenshot of this article.How to Prevent Gutenberg Autop from Messing Up Your Code, Shortcodes, and Scripts
  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS)

Recent Comments

  • Screenshot of the Success, WordPress has been installed page.How to Create a WordPress Website on Amazon EC2 (AWS) (1)
    • Erik
      - Great article. All worked great except for this step:apt install php-mysqlChanging to this fixed it:apt install ...
  • Add Custom Taxonomy Tags to Your WordPress Permalinks (125)
    • Anthony
      - Where does this code go? Like, what exact .php file please?
  • Screenshot of an example article in code view of a modified Gutenberg editor.How to Harness the Power of WordPress Gutenberg Blocks and Combine It with Legacy Free-Form Text (1)
    • tom
      - hi,my experience was like the same, but for me as theme developer the "lazy blocks"(https://wordpress.org/plugins/lazy-blocks/) ...
  • WordPress Custom Taxonomy Input Panels (106)
    • Phil T
      - This is unnecessarily confusing. Why declare a variable with the same name as your taxonomy? Why did you choose a taxonomy ...
  • Create Pop-up Windows in Your WordPress Blog with Thickbox (57)
    • Jim Camomile
      - I have used this tutorial several times and it is one of the few simple and effective ways to add popups with dynamic content, ...

Copyright © 2025 · Genesis Skins by ShibaShake · Terms of Service · Privacy Policy ·