Add Custom Fields in Custom Post Types & Show in Dashboard

So far we have covered half of custom post types and ready to set up or add custom fields in these CPTs. These custom fields are used to add the custom content in WordPress post editor page.

Prior to reading this article, we have come to know about Custom Post Types itself along with setting up default and custom taxonomies in them. For your sake, articles prior as well as after to this one are listed below.

Add Custom Fields in Custom Post Types

Custom taxonomies for custom post type content

Set up or Add custom fields to CPT

Display custom fields content in WP templates

WordPress Custom Fields Meta Box also provides you feature to add custom content yet it’s not enough comfortable. You can indeed customize them still they are time-consuming and less productive. To set up custom fields it’s better to use a plugin and we will set up the fields using Advanced Custom Fields (ACF) plugin here.

Add Custom Fields in Custom Post Types

As always there are many plugins at WordPress plugins repository and you are free to choose whatever comforts you. We have installed and activated ACF plugin and are using it from the menu, Custom Fields in the dashboard.

First of all, create a new field group and give it a friendly name to remember. In the Location meta box, we have to create a set of rules to show this field group at the appropriate location. Here, we can specify to show this field group if the Post Type is equal to the book.

If you’re using the field group at multiple locations, give an Order No. You can also tell where should the custom field group appear at WordPress Post Editor by specifying the Position.

Hide on-screen option can remove selected WordPress default Meta Boxes to input data from the editor. While it’s easier to hide unwanted features from this ACF plugin (editor, comments, revisions, trackbacks etc.), you can also hide these default data inputs by not adding respective core features to the supports key in the arguments passed to register_post_type() function.

If you’re using multiple field groups for a specific post type, the Hide on-screen option will work as per mentioned in the Field Group with the lowest Order No.

Add as many as fields you require in field groups from the Advanced Custom Fields plugin. The plugin itself is rich and has plenty of options to add custom fields. Once you have done, it’s time to add custom admin columns to the CPT list.

Properly Display CPT Posts in WordPress Dashboard

Add Custom Fields Admin Columns

Once you have added a few posts to your CPT, the list of all those looks weird due to lack of more important column headings and related data in them. The All Books list page doesn’t show us any useful information at all. So we need to add a bit more code to our functions.php or plugin file (recommended).

The first step needed is to make these columns available on the edit posts page. WordPress has a variable filter hook called manage_edit-{$post_type}_columns for all post types. Since the name of our post type is the book, the name of the hook would be manage_edit-book_columns.

WordPress shows cb (the checkbox), titleauthorcategoriestagscomments and date columns in the same order for the Post post type by default. You might need to rename a column heading, remove one or more default columns and add custom columns in ‘edit custom post type’ table. Using the hookmanage_edit-book_columns, I’m providing you with a function to perform all those aforesaid actions.

Content to Add Custom Fields in Admin Columns

WordPress knows how to handle the default columns while custom columns need you to render their data. All you have to do is, utilize manage_{$post_type}_posts_custom_column action hook. Since the book is our post type, manage_book_posts_custom_column would be the name of the hook. 

So you can easily supply the value of custom columns using get_post_meta() function. The code above is not ACF specific or you can display all meta fields values regardless of the plugin used. And you can call the same function custom_admin_columns_values() for another hook. For example, manage_event_posts_custom_column for event post type.

Still, if you wish to use the ACF plugin way, the function is below. Please note I have used many variations to obtain the post meta. You can pick and use the way what you like the most.

The last thing to do is display custom post types pages to visitors along with custom fields we’ve added. We are going to cover it in the next part of this tutorial series. Create custom post types and add custom fields to them before we go further.

Also, don’t forget to share this comprehensive series about Custom Post Types if you liked it. Your feedback and comments are also most welcome.

Read next: Display Custom Fields in WordPress Templates.

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.