DsgnWrks

  • Home
  • Code
    • Plugins
      • BibleGateway Links Shortcode
      • CMB2
      • Code Snippets CPT
      • Cubify WP
      • DsgnWrks Importer for Instagram
      • Easy Digital Downloads – Internal Notes for Products
      • DsgnWrks Twitter Importer
      • Enhanced Admin Bar with Codex Search – WordPress Plugin
      • Google Analytics Top Content Widget – WordPress Plugin
      • Hash Link Scroll Offset
      • HTML5 Slideshow Presentation – WordPress Plugin
      • IFTTT Post Formats
      • Jetpack Slideshow Caption
      • Network Sites Counts Dashboard Widget
      • story|ftw
    • All projects
  • Talks
  • Connect
  • About

2 Comments

Posted on September 28, 2015

What is Post Meta? An intro to WordPress Custom Fields

For more background on this post series, read “Post Meta Bootcamp”.
You may have also heard that WordPress has something called “custom fields.” The WordPress Codex says this in relation to custom fields, “This arbitrary extra
information is known as meta-data.” So as you may have suspected, custom
fields, post meta, and meta-data in WordPress are all referring to the
same thing. This arbitrary extra information is saved to a WordPress
post object and is associated with the post ID.

Post meta works the same for all WordPress post objects. What this means is we can store and use post meta for pages or custom post types in exactly the same way we do for actual posts.

But what kinds of things can or should we store as post meta? The short answer: Almost anything. The Codex uses these examples:

  • Mood: Happy
  • Currently Reading: Cinderella
  • Listening To: Rock Around the Clock
  • Weather: Hot and humid

You could also store things like:

  • Post background color: #FF0000
  • Subtitle: My Post Subtitle
  • Show Social Links?: true

The types of information you can store is nearly unlimited, However
if you’re planning on storing categorical information, and don’t want to
use WordPress’ built-in Categories or Post Tags, you may want to forgo
post meta, and add your own custom taxonomy instead.

Custom fields have a built-in rudimentary UI* in the post editor. You may need to enable them.

If you temporarily drop the following snippet into your theme’s functions.php file, you’ll be able to see ALL the custom meta (fields) data associated with a given post from within the post editor page.

function wpsnipp_show_all_custom_fields() {
    if ( isset( $_GET['post'] ) ) {
        $post_id = absint( $_GET['post'] );
        ?>
        <div id="message" class="updated">
            <h3>All post meta:</h3>
            <xmp><?php print_r( get_post_meta( $post_id ) ); ?></xmp>
        </div>
        <?php
    }
}
add_action( 'all_admin_notices', 'wpsnipp_show_all_custom_fields' );

Doing this is a great way to determine if your post meta was properly
saved, or if it exists. Let me walk you through the output of this
snippet via the screenshot that follows.

get_post_custom

  1. This is the post meta ‘key’. This is the identifier for a piece (or
    pieces, we’ll get to that later) of information. From now on, I’ll refer
    to it as the $meta_key. You’ll use that $meta_key in the different function calls:

    • get_post_meta( $post_id, $meta_key, true ) In the above snippet, we used get_post_meta with only one parameter passed in, the $post_id, but generally you’ll want to pass the $meta_key and $single parameter as well. I’ll explain more in number 3.
    • update_post_meta( $post_id, $meta_key, $new_value )
    • add_post_meta( $post_id, $meta_key, $meta_value, $unique )
  2. The underscore at the beginning of these two meta keys tell
    WordPress that they are “hidden” fields, meaning they will not show up
    in the standard Custom Fields editor. The two you see in this screenshot
    are meta data fields that WordPress stores for the post revisions
    feature.
  3. This is the value row of the field you are checking.
  4. Right now you’re seeing a value with only one row, but the ability exists to add multiple rows of values via the add_post_meta function. This is not often used, but can be handy for advanced
    querying of meta data values which is outside the scope of this article.
    When using the get_post_meta function, you must specify
    the third parameter to be true in order to get a singular value, which
    in our case would just be ‘sample field value’. If you don’t specify,
    WordPress will default to giving you all the value rows in an ‘array’
    and you’ll have to separate the values you need yourself. Again, it is
    very rare that you would omit (or pass a false value) to that third
    parameter.

Ok! We’ve covered get_post_meta pretty thoroughly in this article. We’ll continue on in the next article with an in-depth walkthrough of the update_post_meta and add_post_meta functions. Let us know in the comments if anything is still fuzzy to you!

*There are many plugins and code libraries that create customized field inputs and custom metaboxes. An example of a plugin is Advanced Custom Fields, and example of a code library is CMB2 (but it’s also a plugin!).

Part 1 of a 5 part series originally written for wpsnipp.com (1, 2, 3, 4, 5)

Share the love:

  • Facebook
  • Twitter
  • Reddit
  • Email
  • Tumblr

Like this:

Like Loading...

Category: How-to, The Basics Tags: CMB2, custom fields, post meta, Post Meta Bootcamp

← Bookmarklet to Toggle Lists Display in Basecamp
Post Meta.. How do I save it? →

2 Comments on “What is Post Meta? An intro to WordPress Custom Fields”

  1. Michael
    July 11, 2017

    Awesome, I was looking for this… since I was trying to add a Post Meta Key starting with an underscore… your hint to the functions.php gave me the solution I needed, Thanks :)

    Reply
  2. pyaga
    February 23, 2018

    how to add keywords in single post

    Reply

CommentCancel reply

Recent Posts
  • It me!
  • “Is experience exactly the same as pessimism?”
  • WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string
  • Interview on WP Elevation
  • End of an Era: Moving on from WebDevStudios
  • CMB to CMB2 – a migration tale
  • Don’t Repeat Yourself. Use WP Lib Loader instead!
  • What is the future of CMB2?

WordPress Plugin

WordPress Plugin

WordPress Plugin

WordPress Plugin
  • Feeling Generous? Feel free to buy me a coffee! :)
  • Home
  • Code
    • Plugins
      • BibleGateway Links Shortcode
      • CMB2
      • Code Snippets CPT
      • Cubify WP
      • DsgnWrks Importer for Instagram
      • Easy Digital Downloads – Internal Notes for Products
      • DsgnWrks Twitter Importer
      • Enhanced Admin Bar with Codex Search – WordPress Plugin
      • Google Analytics Top Content Widget – WordPress Plugin
      • Hash Link Scroll Offset
      • HTML5 Slideshow Presentation – WordPress Plugin
      • IFTTT Post Formats
      • Jetpack Slideshow Caption
      • Network Sites Counts Dashboard Widget
      • story|ftw
    • All projects
  • Talks
  • Connect
  • About
Twitter
My Tweets
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Comments
  • Add Multiple Products to WooCommerce Cart using the fetch API - Insytful on WooCommerce: Allow adding multiple products to the cart via the add-to-cart query string
  • JT on DsgnWrks Importer for Instagram
  • andrewpschultz on DsgnWrks Importer for Instagram
  • Rory Heaney on DsgnWrks Importer for Instagram
  • TheBeersBeer on DsgnWrks Importer for Instagram

Copyright © 2025 · All Rights Reserved · DsgnWrks

Swell Theme from Organic Themes · RSS Feed · Log in

  • Github
  • Twitter
  • Linkedin
  • dribbble
 

Loading Comments...
 

    %d