Nicola Mustone

Support Lead @ Automattic


Leadership, web, programming. Short essays and hands-on guides, focused on results, not hype.


Custom Fields and How to Use get_post_meta

4–6 minutes
custom red tuned ford mustang at night

Have you ever heard about custom fields or custom meta boxes in WordPress? You probably already use them without realizing it!

Custom fields are one of WordPress’s most powerful features. They let you store extra information (called post meta) about your posts, pages, or products; things like prices, ratings, or custom messages.

Many developers get stuck when it comes to retrieving these values in their themes or plugins. In this article, I’ll explain exactly how to use get_post_meta() to get your custom field data and make the most of it in your projects.

What Are WordPress Post Meta Data?

At its core, post meta (short for metadata) refers to the extra information associated with each post in WordPress. They provide details that aren’t immediately visible on the front end.

A WordPress post meta could be anything, like the price of a WooCommerce product. Or the client of a project for a Portfolio post type. If you used custom fields before, or the plugin Advanced Custom Fields (ACF), those fields’ values are post metadata.

They’re primarily stored in the wp_postmeta table in the database.

The screenshot shows a list of post meta data and their values in a database table
Some post meta in the database. The four columns, from left to right, are the meta ID, the ID of the post they belong to, the name, and the value.

How do Post Meta Improve Your Content?

Post meta data are useful in many cases:

  • Customization: Using post meta, you can tailor your content to fit your needs. Want to add a book review rating to your posts? Post meta makes it easy.
  • Organization: It helps in sorting and filtering content. For example, you can display all posts from a specific author or those with a particular custom field value.
  • Dynamic Content: When you mix post meta with plugins or custom code, they can change how you display the content based on its metadata.

If you are also optimizing your site structure, this guide on WordPress tags SEO explains how to use tags in a way that actually helps search engines and readers.

How to get Custom Field Values with get_post_meta?

WordPress offers the get_post_meta function. You can use it on any post type: a post, page, or product. This function uses three parameters:

  • $post_id: The specific ID of the post.
  • $key: The custom field name, also known as meta key. It is often prefixed with _ to indicate hidden post meta.
  • $single (optional): Determines if the custom field value is singular or an array.

Here is a usage example:

// Optionally retrieve the post object if you don't have it already
global $post;

// Get the value of the post meta with name `_my_post_meta_name`
$post_meta_value = get_post_meta( $post->ID, '_my_post_meta_key', true );

// Print the post meta value
printf( "The value of my post meta is %s", $post_meta_value );

Remember, get_post_meta can be used on any custom post type, not just blog articles or pages.

How to Add or Update Post Meta with Code

To add the meta field values to the database, use the add_post_meta function. This function requires four parameters:

  • $post_id: the post ID. Remember, it can be any post type, not necessarily an actual post.
  • $key: the custom field name.
  • $value: the post meta data value.
  • $single (optional, to avoid duplicate keys): for updating existing custom fields.

Updating WordPress post meta fields works similarly, with update_post_meta function.

How to Update or Add Post Meta in an Existing Post Without Code

Step 1. Access the Post Editor: Navigate to your WordPress dashboard and open the post you want to update.

Step 2. Locate the Custom Fields Metabox: Scroll down below the post editor. If you don’t see the Custom Fields section there, click on Screen Options at the top right of your screen and activate the Custom Fields. If you are using the Block Editor instead, you should go to Preferences (the three dots on top right) > Panels > Custom Fields.

The preferences of a post in the Block Editor showing to click on the tab Panel and the toggling the Custom fields option.

Step 3. Add the Custom Field: In the Custom Fields section, click Enter new to create a new field. Here, you’ll enter a name and value for your custom field. The name is like a label.
It is the $key you’ve seen in the article above. Value is the actual information you’re adding or updating.

  • Name: Choose a name that is descriptive and unique to this particular type of information.
  • Value: Enter the specific data or note you want to add to your post.

Step 4. Save Your Changes: Once you’ve filled out these fields, click Add Custom Field, and don’t forget to save or update your post.

The custom fields section is shown in this screenshot, with some fields and their vaues.

How to Delete Post Meta in WordPress

Deleting WordPress custom post meta works just like adding or updating. The function to delete custom field values is delete_post_meta(). It needs three parameters:

  • $post_id: the post ID.
  • $key: the custom field name.
  • $value (optional): the specific value to delete.

To delete post meta fields without code, reach the custom fields section just like when you added them, and delete them.

Level Up Your WordPress Experience

Now that you’re familiar with the concept of WordPress post meta and how to use them, it’s time to experiment. Start by exploring the custom fields feature in your next post. See how it can add depth and functionality to your content.

Remember, the power of WordPress is in its flexibility and customization. Post meta data are a cornerstone of this capability.

I’d love to hear how you plan to use post meta in your WordPress site. Share your ideas or questions in the comments below!

Cite This Essay

Use this ready-formatted reference in your work or research notes.


7 responses to “Custom Fields and How to Use get_post_meta”

  1. Hi Nicola,

    Great article, and very informative to my limited knowledge of custom fields. I’d appreciate some advice on a certain custom field.
    On my single WooCommerce product page is an inquiry form with the usual contact information fields. There are also two more fields I’ve added: SKU & PRODUCT. The SKU field is working fine and pulling in the associated SKU: [post_meta key=”_sku”]
    However, the product field is not pulling in anything: [post_meta key=”_product”] Is there another word I need to replace ‘product’ or is there an entirely different string of text to apply?
    Thanks, Gareth

  2. When I had tons of meta keys to work with to display some custom content for custom post types, instead of making several dozen calls to the get_post_meta function to grab the keys I wanted, I wrote this function instead. Basically it gets all of the meta keys and filters out all of the arrays with single values while maintaining any arrays that actually do have multiple values and returns an associative array of the results. If you have lots of single values to get, it saves you from having to iterate over every result to get your $k=>$v pairs so you can just access them by $mk[‘my_custom_meta_key’].
    function array_push_assoc($array, $key, $value){
    $array[$key] = $value;
    return $array;
    }

    function filter_gpm($array){
    $mk = array();
    foreach($array as $k => $v){
    if(is_array($v) && count($v) == 1){
    $mk = array_push_assoc($mk, $k, $v[0]);
    } else {
    $mk = array_push_assoc($mk, $k, $v);
    }
    }
    return $mk;
    }
    Call it like so
    $mk = filter_gpm( get_post_meta( get_the_ID() ) );

  3. Thanks for this little nice tutorial

    1. You’re welcome! I’m happy you enjoy it.

  4. Shubhra Vashishtha Avatar
    Shubhra Vashishtha

    thank you so much for the code, it helps me a lot

    1. You’re welcome!

  5. thank you so much for the code, it helps me a lot

Leave a Comment

Discover more from Nicola Mustone

Subscribe now to keep reading and get access to the full archive.

Continue reading