By default, WordPress allows users to create two types of content: Posts and Pages. Essentially, the content of both pages as well as posts is written in the form of paragraph in the WordPress visual editor.
But, what if you need to add more content (i.e. custom content) to your pages and posts?
For instance, the titles of your WordPress post may seem boring to you, as you’re only limited to, adding text to the post titles and cannot add any link or HTML to them. This is where WordPress’ post meta come in handy. It helps to spice post titles by enabling you to add any HTML to the titles.
In nutshell, the post meta is the value of custom fields – a popular WordPress feature that enables users to customize their site, by adding extra bits of information to a post. The type of content that you’re adding, however, could vary depending on the type of WP powered site you’re creating. In this post, we’ll be discussing about how we can add, display on the front-end and utilize post meta on our WordPress website.
How to Add Post Meta?
Adding post meta is fairly simple, all you need to do is to access your WordPress admin panel and open the post edit page where you’ll add post meta to the post. For doing so, click on “Add New” option under the Posts menu and you’ll be able to see the ‘custom fields meta box’ containing the post meta. If not, on the top right of the post edit page, click on “Screen Options”. From there, check the ‘Custom Fields’ checkbox to enable post meta as shown in the screenshot below:
So now that you’ve enabled post meta, you will be able to see at the bottom of your post edit page as shown in the following screenshot:
In the above screenshot, there is a drop down list underneath the ‘Name’ field, click on the list will present all the post meta keys you might have added to any other post. If you want to add the same post meta to your post, then simply choose the key that matches up to your needs. Or else you can define a new post meta key by clicking on “Enter New”.
Let’s say, you want to enter a new post meta with the name as “Price” along with its value “$29.99”. In that case, click the enter new link and add your choice of post meta key and the value. And then, click on the “Add Custom field” button as follows:
On clicking on “Save” or “Publish” the post meta that you’ve created will get attached to your post.
How to Display Post Meta on Theme?
Once we have added the post meta to the post we might need to display it on the post. WordPress provides us with functions with which we can read the post meta associated with a post. To read the post meta of a post you can use the WordPress function get_post_meta.
Now you’ve the post meta added to your post, you may want to show it up on the post in the front-end, i.e. on your WordPress website theme. For doing so, you need to use WordPress functions that allows you to read the post meta attached to your post. This function is called get_post_meta. The function accepts 3 parameters (or arguments) as listed below:
- post ID: This argument specifies the ID of the post having the meta you want to read;
- meta key: It defines the post meta key you’re interested in reading on the post;
- boolean: Passing this argument the ‘true’ value will return the meta value in the form of a string; but if the value is passed as ‘false’ then it will return the meta values in the form of an array.
In order to display your post meta “Price” on the post, you need to add the below code snippet to your theme in the location – where you want your post meta to show up.
$price = get_post_meta(get_the_ID(), ‘Price’, true);
echo ‘Cost of the item : ‘.$price;
Apart from get_post_meta, there are several other useful post meta related functions you should be aware of. Some of these functions are:
- get_post_custom_keys: takes post ID and returns meta keys (associated with that post) as an array.
- get_post_custom: it accepts a post ID as input and returns an array containing all the meta keys and values of that post.
- delete_post_meta: it accepts the post ID along with the meta key you want to delete.
Using Post Meta Together With Custom Post Type
Till now you would have come to know about the usefulness of the post meta feature. If you want to make even better use of this feature, you can use it together with WordPress custom post type feature. Combining both these features help in creating many different types of websites on WordPress tailored to meet your specific needs. In order to use post meta on your custom post type, you just need to add custom fields inside the supports parameter of the WordPress built-in function register_post_type as follows:
‘labels’ => array(
‘name’ => __( ‘Magazines’ ),
‘singular_name’ => __( ‘Magazine’ )
‘public’ => true,
‘has_archive’ => true,
‘supports’ => array(
One of the best things about WordPress that makes it so popular among masses is the flexibility it offers. You can use WordPress not only for plain blogs, but also for creating different types of sites – be it photography site or a corporate site and many more. One great feature that makes a WordPress site more customized is post meta. Thanks to the post meta feature, you can customize your posts and use them for a multitude of sites. What’s more? WordPress even offers several functions to help you utilize post meta in your website themes or plugin.
Hopefully reading this post will serve as a good starting point for you in learning about the process of creating post meta and its other related aspects.