Written July 13, 2011.
Tagged #custom, #order
In this tutorial, we will add 2 extra fields to the page object:
- Page Image: An image field (we can show this as a thumbnail in the column)
- Featured: A true / false field (we can filter the pages via this field)
1. Add extra page fields
Using the Advanced Custom Fields Plugin, add a new ACF group with the 2 fields. Note: The image field needs to save the attachment ID so we can easily retrieve the thumbnail version for the column.
2. Add a sprinkle of code
Now that our pages have extra fields, let’s add in the code to show the fields in the page’s columns. The 2 functions bellow hook into WordPress and override the column creation process. the “my_page_columns” function overrides what the columns are and the “my_custom_columns” function tells WordPress what to do with the columns.
This is what your page columns should look like
3. And a pinch of salt: Sortable Columns
To finish of this tutorial, lets now allow the featured column to be sortable. This can be a very useful feature when working with lots of page / post objects.
Wrapping Up
I hope you enjoyed this simple but useful tutorial. We covered some advanced WordPress & PHP techniques and created an attractive and user friendly admin edit page. Be sure to tweet about this article, thanks for reading.






Sean Rhorer
Seems like a simple tutorial… except there’s no mention of where to make these changes in the code! Any guidance would be greatly appreciated. Thanks!
admin
Hi Sean.
All the code above needs to go into your functions.php file. Good luck
Dan
Hi Elliot
Great tutorial. How can I add this to a Custom Post Type instead? Thanks
Dan
Ignore my previous comment, I should have studied the code a bit harder at first…
add_action(“manage_(your custom post type)_custom_column”, “my_custom_columns”);
add_filter(“manage_edit-(your custom post type)_columns”, “my_page_columns”);
Rory
In response to what Dan said about using this for Custom Post Types – you DON’T need to include your post type on both instances like indicated above.
You instead change “pages” to “posts” on the add_action line:
add_action(“manage_posts_custom_column”, “my_custom_columns”);
Add include your post type name only on the add_filter line:
add_filter(“manage_edit-MY*CUSTOM*POST*TYPE_columns”, “my_page_columns”);
Alberto "Raben" Macaluso
Very interesting tutorial. The idea of creating tutorials for developers is really brilliant! thank you very much
Kien Le
How to add value in php code?
Jonah West
Hey Elliot,
I’m having trouble getting this to work for a custom post type. Here is the code I’ve got:
/*-------------------------------------------------------------------------------
Add Admin Columns and Sorting for Editorials
-------------------------------------------------------------------------------*/
function my_post_columns($columns)
{
$columns = array(
'cb' => '',
'title' => 'Title',
'editorial-author' => 'Author',
'date' => 'Date',
);
return $columns;
}
function my_custom_columns($column) {
global $post;
if($column == 'editorial-author') {
if(get_field('author')) {
the_field('author');
}
}
}
function my_column_register_sortable( $columns )
{
$columns['editorial-author'] = 'editorial-author';
return $columns;
}
add_filter("manage_edit-editorials_sortable_columns", "my_column_register_sortable" );
add_action("manage_editorials_posts_custom_column", "my_custom_columns");
add_filter("manage_edit-editorials_columns", "my_post_columns");
Any ideas?
Thanks,
Jonah
Antoine
I can’t manage to find what goes wrong in this code (I use Blackbox for Debug using Profiler) :
add_action("manage_posts_custom_column", "my_custom");
function my_custom() { apply_filters('debug', 'Profiler Checkpoint'); } // FAILS
add_action("manage_posts_custom_column", apply_filters('debug', 'Profiler Checkpoint in Anonymous function')); // WORKS
Bobe
I love your tutorials!
I have on question, I hope someone can help me.. I customized the code a little bit, adding Taxonomy term. But the thumbnail and the term doesn’t show. The columns are created but without content. Thanks so much!!
function my_page_columns($columns)
{
$columns = array(
'cb' => '',
'thumbnail' => 'Thumbnail',
'title' => 'Title',
'cat' => 'Product Category',
'date' => 'Date',
);
return $columns;
}
function my_custom_columns($column)
{
global $post;
if($column == 'thumbnail')
{
echo wp_get_attachment_image( get_field('product_images', $post->ID), array(200,200) );
}
elseif($column == 'cat')
{
$terms = get_the_terms( $post->ID, 'productcategories');
if ($terms) {
foreach ($terms as $term => $myterm) {
echo 'slug.'&post_type=products">'.$myterm->slug.'';
}
}
}
}
add_action("manage_products_custom_column", "my_custom_columns");
add_filter("manage_edit-products_columns", "my_page_columns");
Bobe
Oh, some code dissapeared. Hope it works this time. It should say
echo 'slug.'&post_type=products">'.$myterm->slug.'';
Bobe
Since my code isn’t interpreted correctly when I publish it here, i’ve created a page where you can see it: http://jakebox.com/test-2
Sorry and thanks!
Maja
How could I get a list of categories to display in a drop-down list so users don’t forgetto select a category?
Thanks,
Maja
Rory
Elliot, you are a god amongst men. Thank you for this article
Mark Murphy
Actually, for a custom post type, the add_action call needs to look like this, where post_type is the singular name you registered. Note ‘_posts_’. My post_type below is ‘message’. Hope this sames someone some time
add_action(“manage_message_posts_custom_column”, “messages_custom_column”);