Thoughts, stories and ideas for web developers (with a focus on WordPress)

Custom Post Types and Custom Fields are an incredibly powerful way to make WordPress meet your specific needs. With the use of 'Admin Columns', you easily visualize custom fields in your WordPress admin dashboard.


For this demo, I set up a clean WordPress installation. I created a Custom Post Type called "Call requests" and attached a custom field to this post type called "Phone number". The screenshots show "belverzoeken" and "telefoonnummer", but this is Dutch for "call requests" and "phone number". Now you're both learning Dutch and WordPress!

The challenge

A client wants to expand her website with a "call request" form. Users can drop their phone number and a short description of their question. After submitting the form, all the data gets saved in the custom post type "call requests". To make it easier for the client to process call requests, we want to make the phone number accessible in the WordPress dashboard. This way, she doesn't have to open the post to check the phone number.

The solution

We're going to need two separate pieces of code. The first piece of code will create the custom admin columns, while the second piece of code will fill the columns with the relevant data of the custom field.

Creating admin columns

Admin columns are always attached to a custom post type. So in the code below, make sure to change 'callrequest' to the name of your custom post type. We'll use the WordPress filter manage_posttype_post_columns to create the extra admin columns.  

I'll only add 1 admin column, but in the code below I'll add 2 just so you can see how to add multiple admin columns if you should need it.

function add_columns_callrequest ( $columns ) {
    return array_merge ( $columns, array (
        'phone_number' => 'Phone number' ,
        'callrequest_status'   => 'Status' 
    ) );
add_filter ( 'manage_callrequest_posts_columns', 'add_columns_callrequest' );

So how does it work:

  • The function add_columns_callrequest gets a parameter of $columns. This contains an array of all the columns of the (custom) post type.
  • With the PHP function array_merge, we'll expand the array with custom columns. For every column, we'll add a key and a value. The value will be shown as the column title in the WordPress dashboard.
The code adds a new column to our custom post type, but we'll still need to fill it with data. 

Populating the columns with custom data

To fill the columns, we'll use the WordPress action manage_posttype_posts_custom_column. It's crucial to replace 'posttype' with the name of your custom post type.

function callrequest_custom_columns ( $column, $post_id ) {
    switch ( $column ) {
        case 'phone_number':
            echo get_post_meta ( $post_id, 'phone_number', true );
        case 'callrequest_status':
            echo get_post_meta ( $post_id, 'callrequest_status', true );
add_action ( 'manage_callrequest_posts_custom_column', 'callrequest_custom_columns', 10, 2 );

So how does it work:

  • The function will go through all the columns for the custom post type.
  • With a switch, we go through all the columns and check the keys.
  • If the key is equal to the key of the column we'd like to alter, we echo the value that we would like to fill the column with. In my case, I'll get the custom field 'phone number' and 'callrequest_status'.

That's all folks. The admin column will now be dynamically populated with the right data.

You’ve successfully subscribed to Teebow Blogs
Welcome back! You’ve successfully signed in.
Great! You’ve successfully signed up.
Success! Your email is updated.
Your link has expired
Success! Check your email for magic link to sign-in.