Post Meta Fields And The get_post_meta Function In WordPress

WordPress get_post_meta function

Did you know that any post, page, or another custom post type in WordPress can have custom meta fields? These are fields that can store almost any kind of data about your post.
In this article, we’re going to learn how to use these fields as well as the get_post_meta function.

What Is A Custom Field In WordPress?

Custom Fields in WordPress are a somewhat advanced feature that lets you add extra information to specific posts or post types. This information is called metadata.

Custom meta fields are stored in the wp_postmeta table in your database which looks like this:

wp_postmeta database table

An example of using the custom fields would be a scenario where you’d only want to display certain posts on the homepage.

You can store that information in a custom field named “show_on_homepage” that can have a value of 1 or 0.

Having that information you can then easily query only the posts that have to be displayed on the homepage with the help of the meta query in WordPress or by using the get_posts function.

How To Use get_post_meta Function

WordPress provides a function called get_post_meta that helps you get custom data for your post. The function is defined in a following way:

<?php

function get_post_meta($post_id, $key, $single = false);

Here is what each parameter means:

  • $post_id – ID of the post that you want to get custom value for.
  • $key – the name of your field
  • $single – whether to get a single value or an array of values. By default this parameter is false so an array will be returned

As you can see, the get_post_meta can return either a single value or an array of values for a given post based on your meta key.

Now, let’s see how we can use that function.

Let’s assume that we want to check if a post with the ID of 1 should be shown on the homepage which is stored in as “show_on_homepage” meta field in the database:

<?php

$showOnHomepage = get_post_meta(1, "show_on_homepage", true);

if ($showOnHomepage == 1) {
    // Do something with it
} 
else {
    // Otherwise do something else
}

Adding, Updating, And Deleting Post Meta

Besides the get_post_meta function, WordPress also has a function to add post meta (add_post_meta), update post meta (update_post_meta) and to delete post meta (delete_post_meta).

With the use of these functions, you can easily store and manage any custom fields that you need.

WordPress Plugins For Better Custom Fields Management

While adding custom fields from code is oftentimes helpful, in some scenarios you may want to allow your admin user to define and store custom data in a visual way.

There are quite a few plugins that will allow you to do that, but the major “players” are:

Conclusion

Custom Fields are a powerful mechanism to store any information about your posts, pages, and other post types. With the help of get_post_meta and other meta functions you have ultimate flexibility in your code to do whatever you want with those fields.

About The Author

Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

The Ultimate Managed Hosting Platform

FREE TRAINING

How To Pick The Right Platform For Your Dream Course Or Membership Site

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.