WP Custom Field

Last Updated: March 10, 2022

Standard post in WordPress has only the main content area to enter the text to your post but when you need to add some custom piece of data to the post thesis there is no place to enter those details.

So you have the custom field to solve this problem.

You can add a custom field to post when creating a new post or edit an existing post in WordPress dashboard.

WP stores the custom fields as key and value pairs.

Now let’s see how to create the custom field name post_name where you can give different name than the existing title of the post.

How to create custom field

First you can create a new post or edit an existing post inside the  WordPress admin.
You can see the following custom field Metabox below the post

Custom field metabox
Custom Field Metabox

Now you can enter new custom field in to the following form

Add New custom field
You can add custom field here

How to get custom field value?

get_post_meta( int $post_id, string $key = '', bool $single = false )

You can retrieve a post meta field for the given post ID.

You can use the following function to get the custom field name.

$key_1_value = get_post_meta( get_the_ID(), 'post_name', true );
// Check if the custom field has a value.
if ( ! empty( $key_1_value ) ) {
   echo $key_1_value;