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 Cancel reply

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

Comments

« Previous 1 2 3 4 5 Next »
  1. Andrea says

    July 15, 2011 at 2:08 am

    great tutorial!
    i have a question: i want to let the user order post by a custom field value, insted of simply show the value of that custom field.
    Thank you

    Reply
    • ShibaShake says

      July 18, 2011 at 12:38 pm

      Use the manage_{$screen->id}_sortable_columns filter. E.g., ‘manage_edit-gallery_sortable_columns’.

      Reply
  2. Nuwanda says

    April 13, 2011 at 1:52 am

    Ok, this sorta works for my custom post type.

    But I have a custom taxomony instead of the normal wordpress categories.

    How do I show the custom taxonomy that the post belongs to?

    If my custom taxonomy is Cars, I’m using…

    $new_columns[‘cars’] = __(‘Car’);

    ..but all I’m getting is the header and no taxonomy listed.

    Reply
    • ShibaShake says

      April 17, 2011 at 11:54 am

      You need to fill in the column values yourself by hooking into the manage_{$post_type}_posts_custom_column action.

      Check out section 3 in the article above.

      Reply
      • Trewknowledge says

        June 8, 2011 at 2:52 pm

        I am also having a problem displaying the right category. I have a custom post called “slider” so I wrote out
        manage_slider_posts_custom_column

        However all of the posts say “Uncategorized”. All of the posts have a “Slider Category” associated with it.

        Any thoughts?

      • ShibaShake says

        June 13, 2011 at 2:52 pm

        It is difficult to say without looking at the code.

        First, I would just do an “echo blah” to see if your manage_slider_posts_custom_column function is being called. If the function is being called, then likely, there is some bug while extracting the category values from the slider objects.

  3. Dan says

    March 28, 2011 at 5:06 am

    Hi, I just used part of this code to add thumbnails to my custom post types. I used get_the_post_thumbnail($post-ID,array(100,100)); in the switch part for a column I made called image. Worked an absolute treat.

    Epic post, thanks.

    Reply
    • Dan says

      March 28, 2011 at 5:07 am

      whoops, I meant $post->ID. Im always missing the “>” out, bad habit!

      Reply
  4. Adedoyin Kassem says

    February 28, 2011 at 12:34 am

    Thanks for this post, it has been very enlightening. However i have got just one challenge using this feature. I have a custom post type for events and i use this code (below) to create custom columns:

    function add_new_events_columns($event_columns) {
    $new_columns[‘cb’] = ”;
    $new_columns[‘title’] = _x(‘Event Name’, ‘column name’);
    $new_columns[‘location’] = _x(‘Event Location’, ‘column name’);
    $new_columns[‘events_date’] = _x(‘Event Date’, ‘column name’);
    $new_columns[‘date’] = _x(‘Date Posted’, ‘column name’);
    return $new_columns;
    }

    The columns show perfectly well. However, when i tried to call out the content of the location meta box using this code:

    function manage_events_columns($column_name, $id) {
    global $wpdb;
    switch ($column_name) {
    case ‘location’:
    $location_meta = get_post_meta($post->id, ‘location’, true);
    echo $location_meta;
    break;
    }
    }

    I can’t seem to use this method to get the content of the meta boxes to show in the respective columns. Please, i need help on this.

    Reply
    • ShibaShake says

      February 28, 2011 at 7:50 am

      In the code above you did not set $post. Try just using $id.

      Reply
  5. Matt says

    February 26, 2011 at 8:00 am

    Is it possible to add custom meta field values to the columns? I would like to set _lastname, _firstname as the title. Thanks Shibashake!

    Reply
    • ShibaShake says

      February 28, 2011 at 7:52 am

      You should be able to display whatever you want by putting it into the manage_gallery_columns function.

      Reply
  6. Joe says

    January 4, 2011 at 3:13 pm

    Just confirmed: everything is fine in 3.1 RC2 except that If the custom post type is registered as ‘hierarchical’ => true, then the column will not populate with the data.

    Reply
    • Kcssm says

      January 7, 2011 at 2:54 am

      Yes I also got the same problem. The columns doesnot get populated when the `‘hierarchical’ => true` is set. Looking for a solution.

      Thanks..

      Reply
      • ShibaShake says

        January 10, 2011 at 10:29 pm

        For hierarchical custom post types in WP 3.1 try using –
        add_action(‘manage_pages_custom_column’, ‘manage_gallery_columns’, 10, 2);

        or you can also use –
        add_action(“manage_{$post->post_type}_posts_custom_column”, ‘manage_gallery_columns’, 10, 2);

      • alex chousmith says

        February 7, 2011 at 10:48 pm

        in WP 3.0.4, this is also the solution to this problem.

        basically, if your custom post type IS hierarchical, the action hook is “manage_pages_custom_column”. if you did not specificy “hierarchical” => true in the register_post_type , it is by default FALSE, and you can hook to custom columns via the “manage_posts_custom_column” as shown in this post

        good times

  7. Joe says

    January 4, 2011 at 2:33 pm

    Seems like WordPress 3.1 RC2 is having issues echoing the value. Was fine on RC1. Anyone else notice?

    Reply
  8. Team Roster says

    October 29, 2010 at 5:51 pm

    You you could edit the webpage name title
    Add Custom Post Type Columns to something more catching for your webpage you write. I loved the post yet.

    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 ·