Custom Fields In WordPress
Very precise explanation about adding custom field in wordpress
Table of content:-
What is meta or custom field in WordPress
How to add meta or custom fields
What is meta or custom field in WordPress:-
In WordPress, a meta field (also called post meta or custom field) is extra information attached to a post, page, or custom post type.
It is additional data stored about a post that isn’t part of the main content or title. In the wordpress database it’s stored as a key–value pair associated with a specific post in the wp_postmeta table.
AS in a next post discussed about adding Advance custom fields in wordpress post here we are going to discuss details about basic Custom Fields
How to add meta or custom fields:-
> Login to wordpress as admin user
> Add a new post, with title “Car” & some description

let’s say you are going to add a post about a car & you want to add 2 custom fields “Brand” & “Model” go through following steps to enable and add custom fields for your post.
> Click on “Options”
> Click on “Preferences”
> On the the “Custom fields”

Once you ON the “Custom fields” it will ask you to “Show & Reload Page” click it button

Once the page is reloaded it will show the interface to add custom fields.

> Click on “Enter new” button

> Add field “Brand” and its value “Toyota”
> Click on “Add Custom field” to add an other file “Model” and its value “V82025”

Then click on “Publish”

After publishing the post you will notice in view mode custom fields “Brand” & “Model” not visible.
Actually for showing these custom fields on post’s view mode it needs to add a shortcode in your theme’s functions.php file or in some custom plugin file. We have added in the theme’s functions.php file.
// Shortcode to display the ‘Brand’ meta field
function ttf_brand_meta_shortcode() {
// Get current post ID
$post_id = get_the_ID();
// Get the meta field value for key ‘Brand’
$brand = get_post_meta($post_id, ‘Brand’, true);
// Return the value safely
return esc_html($brand);
}
add_shortcode(‘brand_meta’, ‘ttf_brand_meta_shortcode’);
// Shortcode to display the ‘Model’ meta field
function ttf_model_meta_shortcode() {
// Get current post ID
$post_id = get_the_ID();
// Get the meta field value for key ‘Model’
$model = get_post_meta($post_id, ‘Model’, true);
// Return the value safely
return esc_html($model);
}
add_shortcode(‘model_meta’, ‘ttf_model_meta_shortcode’);
> Then on post’s editor add 2 columns
> Then in 1st column add shortcode [brand_meta] & in 2nd column add shortcode [model_meta]

Finally view your post, you will see the “Brand” & “Model” custom fields.

