How to Add Custom Field in Custom Post Type Programmatically

When it comes to adding custom fields in custom post type in WordPress, there are great plugins like ACF, Metabox, and a few others available, but sometimes you just want to add custom fields via code or without using any plugin.

In this article, first, we will take a look at enabling custom fields for custom post types then adding custom fields in custom posts, and then getting custom field value in the front end.

Enable Custom Fields in Custom Post Type

Whether you are creating a new custom post type or you have the existing one, To use custom fields, first you have to enable the custom field support for your post type.

There are two ways to enable the custom field option for the custom post type.

1. Enable Custom Fields With Registering Custom Post Type

If you are creating a new custom post type, it’s always a good practice to enable custom fields while registering your post type.

And to do that, you can pass the 'custom-fields' as an argument within the array of supports parameter of your custom post type, as shown in the code below.

$args = array(
        'label'               => __( 'Events', 'twentytwentyone' ),
        'description'         => __( 'Event news and reviews', 'twentytwentyone' ),
        'supports'            => array( 'title', 'editor', 'custom-fields'),
        // Rest of the arguments goes here
 );
      
// Register Custom Post Type
register_post_type( 'custom-post-type-name', $args );

And this will enable the custom fields option for your custom post type.

2. Enable Custom Fields With Post Type Support

The second way works in both cases: either you are enabling the custom fields option for your existing custom post type or creating new ones.

Cause with the second way, you don’t have to find the code where your custom post types getting registered. And this helps you a lot when you are working with existing custom post types.

In this case, you just need to add the code given below to your theme’s functions.php or a plugin-specific file and add your custom post type name to it.

// Enable Custom Fields Support For Custom Post Type
function diwp_enable_custom_fields(){
	// Replace 'events' with your custom post type name
	add_post_type_support( 'events', 'custom-fields' );
}
add_action( 'init', 'diwp_enable_custom_fields' );

Now, After using any of the above methods, if you are not seeing a custom field option in your custom post type edit page, then make sure it should not be hidden from screen options.

Go to Screen Options and select the checkbox for the Custom Fields as shown in the image below.

custom-fields-not-showing-fixed

Add Custom Field in Custom Post Type

To add a custom field in your post, add the name of your custom field with its value and click on the Add Custom Field button.

This will create a custom field and the name you gave here for a custom field, will help you to identify and get the value in the front end.

add-custom-field-in-custom-post-type

You can also add multiple values for a single custom field by using the same custom field name. As shown in the image below.

add-custom-fields-with-multiple-values

Get Custom Field Value in Front-end

Now, to display the custom field value, add the below code to your post template files like single.php or in your custom post type specific template files, where you want it to show.

If you want to get and display the single value of the custom field, add your custom field name in the below code and set the last parameter True

// add your custom field name 
echo get_post_meta($post->ID, 'your-custom-field-name', true);

If you want to get and display the multiple values of the custom field, add your custom field name in the below code and set the last parameter False.

// add your custom field name
$fields = get_post_meta($post->ID, 'your-custom-field-name', false);
foreach ($fields as $field) {
  echo $field.' , ';
}
Owner of diveinwp.com, solely responsible for creating helpful, informative content about WordPress, a fulltime WordPress developer with 8+ years of hands-on experience in WordPress design and development.