Custom fields and how to use get_post_meta

Did you know about custom fields in WordPress? Most probably you already use them and you don’t know that they are called so.

Some time ago I wrote an article about how to add fields to products in WooCommerce. Those fields are custom product fields, and their values are post meta.

If you check the article and its comments, you will notice that many people were confused about how to get their value, so let me explain to you what they are and how to get them!

What is a custom field?

Custom fields are also known as metadata or post meta in WordPress.

They can be defined as data that describe posts. For example, the post type is a metadata or the post format. In WooCommerce, the price is a custom field, or the product type.

Most of the time they are stored in the database table wp_postmeta, except for some specific data, like the post type.

These data simply describe a post, they add details to it so you can do different things based on those details. For example, based on the post type we use the post in a different way or based on the post format, we show the post in a different way.

Getting custom fields values with get_post_meta

WordPress has a function to get custom field values, get_post_meta().

This function can be used for any post type, it does not have to be a post, it can be a page, a product, or whatever.

It accepts three parameters:

  • $post_id – The ID of the post from which we want to get the custom field value.
  • $key – The name of the custom field. Usually, they start with a _ indicating that it’s a hidden post meta.
  • $single (optional) – Indicates if the custom field value is an array or not. By default the value for this parameter is false, but most of the time you will have to use true.

Here is a usage example:

<?php
// Get the value of the post meta with name `_my_post_meta_name`
$post_meta_value = get_post_meta( $post->ID, '_my_post_meta_name', true );
// Print the post meta value
printf( 'The value of my post meta is %s', $post_meta_value );
view raw functions.php hosted with ❤ by GitHub

Adding and Updating Post Meta

Saving a custom field value in the database is as easy as getting it. The function to use is add_post_meta().

It accepts four parameters:

  • $post_id – The post ID.
  • $key – The custom field name.
  • $value – The value of the custom field.
  • $single (optional) – Whether the same key should not be added if it already exists.

To update the value of an existing custom field use the function update_post_meta() in the same way of the function add_post_meta().

Deleting custom fields values

As you might guess, the function to delete custom fields values is delete_post_meta() and it accepts three parameters:
* $post_id – The ID of the post.
* $key – The name of the custom field.
* $value (optional) – The value of the custom field.


More Posts That You Might Like…


6 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!

Leave a Reply

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at WordPress.com.

%d bloggers like this: