Add Custom Column on Post Admin Panel

  • iftekhar
  • Nov 27, 2014
  • 0 comment
  • 2205 Views
  • Print

By default you would see some basic column (title, date, author, category, tags etc.) on Post administration panel in WordPress. However, there are ways to add custom column. Such feature could really be useful if you have custom post meta box to store additional post meta data.

Today, In this I will show you how to add custom columns in Post Admin panel and display relevant data on those column for every single post. So, lets get started.

Create Custom Columns

The very first thing that we need to do is to create a function that would add extra columns on our post administration panel. To do that we can use the following function.

<?php
function add_custom_post_column($columns) {
   $columns['post_modified'] = 'Modified';
   $columns['post_views'] = 'Views';
   return $columns; }
add_filter('manage_posts_columns','add_custom_post_column');
?>

Once you add this code on your theme’s functions.php file. Go back to your post admin panel and refresh the page, you should see your newly created column with “Modified” and “Views” title.

Displaying Post Meta Data

Since we are done with adding colmuns, lets focus on the function that would display the relevant data on specific column for each post. In this case, its the post modified date and post views count for every single post that we would be working on. That can easily be done using the following function. Let’s take a look at the function.

<?php
function get_post_column_value($value,$post_id) {
switch($value) { 
   case 'post_modified' : echo the_modified_date('Y/m/d');
   break; 
   case 'post_views' : echo get_post_meta($post_id,'views_count',true);
   break; } }
add_action('manage_posts_custom_column','get_post_column_value', 10, 2);	
?>

Simply copy and paste these two snippet on your theme’s functions.php page and update it. You should see the post modified date on your newly added column. This function is particularly very important because this is where you can retrieve any kinds of data related to the post (could be meta data as well).

In one of my previous post I showed, how to display post views count without any plugin. On that example, I created function to record the views count as a post meta data. So, to display such meta data on admin column you can change the “echo” statement with the following line:

echo get_post_meta($post_id,'views_count', true);

If you are planning to display different meta data, simply change the “views_count” meta key with your desired one and also make necessary changes (on first function that creates the actual column) to display appropriate column title.

Multiple Columns

To add multiple columns, all you need to do is to add another line within the function with unique column name and title. Take a look at the following example:

$column['column_name'] = 'Column Title';

You can pretty much add as many column as you want. However try not to overwhelm the admin panel with too many columns. Adding excessive columns may have negative effect on your admin panel UI in terms of loading your posts.

Adding columns also requires you to make necessary adjustment to the function that displays relevant data. So basically every single new column you add, you must add a new “case..break” statement within “switch” control structure like this:

case 'column_name' : echo relevant_post_data; break;

One this line, “relevant_post_data” is the place where you should place your desired function that will pull the desired data of that post. I hope you understood what I just explained.

Explanation

If you are following everything carefully what I have written so far, you may have noticed that I intentionally skipped two most important section of our functions. That is, “add_action” and “add_filter” portion of our custom functions. On our “add_custom_post_column” function, I have used “add_filter” which hooks “manage_posts_columns” filter actions with our custom function. Sounds complicated? In a simple sentence, it is telling “manage_posts_columns” filter what to do with our function. This is a crucial piece of our function because without this, WordPress has no way of knowing that what exactly we want to add as the title of our custom column on default “Posts” admin panel.

On our “get_post_column_value” function we are using “add_action”. It is being used to hook our function with “manage_posts_custom_column” action. In other words, it is actually displaying our desired meta data associated with the post based on the “switch” control structure.

In our function we created two column to display both the post modified date using a function called the_modified_date with desired date and time format structure and post views count using get_post_meta function with desired post meta key “views_count” for every single post. You can surely call your custom “meta key” and get creative.

Column for Custom Post Type

Up until this point our discussion was limited to the default “Posts” admin panel section. Now, what about custom post type. As far as the coding goes, CPT (Custom Post Type) does require very little change on our actual snippet to add custom column. Perhaps, it is the CPT where you would more likely be adding custom column since most people uses “Posts” section to write blog posts only.

To add custom column on CPT admin panel, simply change “manage_posts_columns” with the “manage_CPT_posts_columns” on add_filter section and “manage_posts_custom_column” with “manage_CPT_posts_custom_column” on add_action section. On both cases, change the CPT word with your post type name. Rest of the function should remain as it is. However, if you have custom column for both the “Posts” and “CPT”, change the name of our functions to keep them unique.

That’s all for today folks. If you have any question regarding this topic, feel free to use the comment section below. Thanks.

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from Postable, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.