Last Updated on November 6, 2020 by Abdullah Al Mahamud
If you’ve been working with the WordPress CMS for a while, then you’ll probably be aware of the fact that the CMS comes with two types of standard data types, namely: Posts and Pages. And both the data types allow you to write content in WordPress visual editor. But, you might not to add custom content to these data types, as your website starts to grow in size.
For instance, when adding titles to a WP post, you can only add text and aren’t allowed to add any links. But with the help of WordPress’ post meta (also referred to as custom fields), you can spruce up your post/page titles, as it lets you embed HTML to the titles.
The WordPress’ post meta feature helps to customize a website, as it enables adding additional information to a post. In this post, we’ll be seeing ways to add and display post meta to your website admin dashboard, and how you can make the best use of this feature.
Understanding the Process of Adding Post Meta in Dashboard
The process of adding post meta in your WordPress admin dashboard is very easy. You need to begin with, getting logged in into your website dashboard. Next, open up the post where you would like to add the post meta. And then, navigate to the Posts menu on the left side of your post edit page and click on “Add New”. You will be able to view “custom fields meta box” which will be containing your post meta.
But, in case it does not contain any post meta, at the top of your post edit page on the right, click the “Screen Options” tab and the following window will pop up, where you need to get the “Custom Fields” checkbox checked:
After performing the above steps, the post meta on your post will be enabled. And, you can view the post meta given at the bottom of your post edit page that looks something like:
As you can see in the screen shot above, a drop down list appears just below the ‘Name‘ field. Simply click on that list and it will provide you with all of the post meta keys – already added to other existing posts. Just pick any existing key from that list, or click on “Enter New” for defining a new post meta key.
Let us assume, you would like to enter a new post meta containing “Price” as its name and a value of “$29.99”. If that’s the case, simply click on enter new to add your choice of post meta key and its value. Lastly, click on “Add Custom field” as shown in the screen shot below:
After clicking on the “Save” or “Publish” option, the post meta will attach to your post.
Displaying Your Post Meta on Front-end
After getting your post meta attached to the post, you’ll certainly want to show it up on your live post (i.e. on the front-end). WordPress comes with a default function called
get_post_meta(), giving us the ability to view the post meta related to a post. This function is passed three parameters (or arguments) as follows:
- post ID: determines the post ID containing the meta you want to read;
- meta key: specifies the key of post meta;
- boolean: when it is passed “true”, the boolean argument returns the meta value as string; and when ‘false’ value is passed then this argument returns meta value consisting of an array.
$price = get_post_meta(get_the_ID(), ‘Price’, true);
echo ‘The price of the item is : ‘.$price;
Remember that other than the
get_post_meta() function, you can choose to use many other functions associated with the post meta in WordPress such as the ones mentioned below:
• get_post_custom_keys: it accepts post ID and returns the meta keys in the form of an array that are related to that post.
• get_post_custom: it also takes post ID as input, however it returns an array with all the meta keys and values of that post.
• delete_post_meta: it takes up input in the form of post ID together with the post meta key that needs to be deleted.
Combining the Power of Post Meta With Custom Post Type
Hopefully by now, you would most likely have come to know why you need to use the post meta feature. But to make the best use of this feature, you will need to combine it with WordPress “custom post type” feature. Doing so, will help you create diverse types of sites on WordPress that befits your needs.
Now for using post meta on your custom post type, simply add custom fields within the “supports” parameter within
register_post_type() – WordPress default function:
<?php register_post_type( 'Magazines',
‘labels’ => array(
‘name’ => __( ‘News_Magazines’ ),
‘singular_name’ => __( ‘News_Magazine’ )
‘public’ => true,
‘has_archive’ => true,
‘supports’ => array(
WordPress has become one of the most recommended content management system across the world, owing to the amazing flexibility and tons of customization options it offers.
This CMS is not only used for building blogs, but also for developing different types of websites. Among its rich set of features, one great feature that we have covered in this post is post meta, which helps in customizing WordPress posts.
Going through this post, will provide you valuable insight on WordPress post meta feature.